Skip to content

Commit 34a2b60

Browse files
authored
Add code example for CA2251 rule (#49091) (#49092)
1 parent d21564c commit 34a2b60

File tree

1 file changed

+13
-0
lines changed
  • docs/fundamentals/code-analysis/quality-rules

1 file changed

+13
-0
lines changed

docs/fundamentals/code-analysis/quality-rules/ca2251.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ The result of a call to <xref:System.String.Compare%2A?displayProperty=nameWithT
3232

3333
To fix violations of this rule, replace the expression comparing the result of <xref:System.String.Compare%2A?displayProperty=nameWithType> with a call to <xref:System.String.Equals%2A?displayProperty=nameWithType>.
3434

35+
## Example
36+
37+
```csharp
38+
string leftValue = "...";
39+
string rightValue = "...";
40+
41+
// This code violates the rule.
42+
bool areEqualUsingCompare = string.Compare(leftValue, rightValue, StringComparison.OrdinalIgnoreCase) == 0;
43+
44+
// This code satisfies the rule.
45+
bool areEqualUsingEquals = string.Equals(leftValue, rightValue, StringComparison.OrdinalIgnoreCase);
46+
```
47+
3548
## When to suppress warnings
3649

3750
It is safe to suppress warnings from this rule.

0 commit comments

Comments
 (0)