Skip to content

avoid casts by using pattern matching#4014

Open
SimonCropp wants to merge 1 commit intodotnet:mainfrom
SimonCropp:avoid-casts-by-using-pattern-matching
Open

avoid casts by using pattern matching#4014
SimonCropp wants to merge 1 commit intodotnet:mainfrom
SimonCropp:avoid-casts-by-using-pattern-matching

Conversation

@SimonCropp
Copy link
Contributor

No description provided.

@SimonCropp SimonCropp requested a review from a team as a code owner March 6, 2026 13:35
Copilot AI review requested due to automatic review settings March 6, 2026 13:35
@github-project-automation github-project-automation bot moved this to To triage in SqlClient Board Mar 6, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors several type checks to use C# pattern matching (is T t) in order to avoid repeated casts and improve local readability in core TDS write/serialization paths.

Changes:

  • Replace is + explicit cast patterns with is <Type> <local> pattern matching in TdsParser.
  • Use pattern matching in SqlParameter type conversion logic to avoid as cast.
  • Use pattern matching in SqlBulkCopy conversion to avoid an XmlReader cast when wrapping in XmlDataFeed.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs Replaces several is/cast sequences with pattern matching locals in RPC parameter writing, bulk copy size calculation, and value serialization.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlParameter.cs Uses pattern matching to capture SqlParameter for InstanceDescriptor conversion.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs Uses pattern matching for XmlReader handling when converting XML/JSON values to XmlDataFeed.

Comment on lines +12540 to 12543
if (value is SqlChars sqlCharsA)
{
string sch = new string(((SqlChars)value).Value);
string sch = new string(sqlCharsA.Value);

Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suffix "A" on sqlCharsA isn’t meaningful here and reduces readability. Since the pattern variable is scoped to the if block, consider renaming to sqlChars (or another descriptive name) without the letter suffix.

Copilot uses AI. Check for mistakes.
Comment on lines +12586 to +12588
if (value is SqlChars sqlCharsB)
{
return WriteCharArray(((SqlChars)value).Value, actualLength, offset, stateObj, canAccumulate: false);
return WriteCharArray(sqlCharsB.Value, actualLength, offset, stateObj, canAccumulate: false);
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlCharsB uses a letter suffix that doesn’t convey meaning. Consider renaming to sqlChars to match usage elsewhere in the codebase and keep the pattern variable names consistent.

Copilot uses AI. Check for mistakes.
Comment on lines +13636 to +13638
if (value is SqlBinary sqlBinary2)
{
Buffer.BlockCopy(((SqlBinary)value).Value, offset, b, 0, actualLength);
Buffer.BlockCopy(sqlBinary2.Value, offset, b, 0, actualLength);
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlBinary2 uses a numeric suffix that doesn’t add meaning (it’s already scoped to this block). Consider renaming to sqlBinary for clarity and consistency with other pattern variables in this file.

Copilot uses AI. Check for mistakes.
Comment on lines +13689 to 13692
if (value is SqlChars sqlCharsC)
{
String sch = new String(((SqlChars)value).Value);
String sch = new String(sqlCharsC.Value);
return SerializeEncodingChar(sch, actualLength, offset, _defaultEncoding);
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlCharsC introduces a letter suffix that isn’t meaningful. Consider renaming to sqlChars (the variable is block-scoped) to improve readability.

Copilot uses AI. Check for mistakes.
Comment on lines +13712 to +13714
if (value is SqlChars sqlCharsD)
{
return SerializeCharArray(((SqlChars)value).Value, actualLength, offset);
return SerializeCharArray(sqlCharsD.Value, actualLength, offset);
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlCharsD uses a letter suffix that doesn’t convey intent. Consider renaming to sqlChars to keep pattern variable naming consistent and easier to scan.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

2 participants