Skip to content

Commit 9819caf

Browse files
authored
Ca2251 (#24463)
1 parent 05e0d38 commit 9819caf

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: "CA2251: Use `String.Equals` over `String.Compare`"
3+
description: "Learn about code analysis rule CA2251: Use `String.Equals` over `String.Compare`"
4+
ms.date: 05/31/2021
5+
ms.topic: reference
6+
f1_keywords:
7+
- CA2251
8+
- UseStringEqualsOverStringCompare
9+
helpviewer_keywords:
10+
- UseStringEqualsOverStringCompare
11+
- CA2251
12+
author: NewellClark
13+
---
14+
# CA2251: Use `String.Equals` over `String.Compare`
15+
16+
| | Value |
17+
|-|-|
18+
| **Rule ID** |CA2251|
19+
| **Category** |[Usage](usage-warnings.md)|
20+
| **Fix is breaking or non-breaking** |Non-breaking|
21+
22+
## Cause
23+
24+
The result of a call to <xref:System.String.Compare%2A?displayProperty=nameWithType> is compared to zero.
25+
26+
## Rule description
27+
28+
<xref:System.String.Compare%2A?displayProperty=nameWithType> is designed to produce a total-order comparison that can be used for sorting. If you only care whether the strings are equal, it is both clearer and likely faster to use an equivalent overload of <xref:System.String.Equals%2A?displayProperty=nameWithType>.
29+
30+
## How to fix violations
31+
32+
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>.
33+
34+
## When to suppress warnings
35+
36+
It is safe to suppress warnings from this rule.
37+
38+
## See also
39+
40+
- [Performance warnings](performance-warnings.md)

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

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ The following table lists code quality analysis rules.
181181
> | [CA2248: Provide correct enum argument to Enum.HasFlag](ca2248.md) | The enum type passed as an argument to the `HasFlag` method call is different from the calling enum type. |
182182
> | [CA2249: Consider using String.Contains instead of String.IndexOf](ca2249.md) | Calls to `string.IndexOf` where the result is used to check for the presence/absence of a substring can be replaced by `string.Contains`. |
183183
> | [CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has. |
184+
> | [CA2251: Use `String.Equals` over `String.Compare`](ca2251.md) | It is both clearer and likely faster to use `String.Equals` instead of comparing the result of `String.Compare` to zero. |
184185
> | [CA2300: Do not use insecure deserializer BinaryFormatter](ca2300.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
185186
> | [CA2301: Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder](ca2301.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
186187
> | [CA2302: Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize](ca2302.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |

docs/fundamentals/code-analysis/quality-rules/usage-warnings.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ Usage rules support proper usage of .NET.
5353
|[CA2248: Provide correct 'enum' argument to 'Enum.HasFlag'](ca2248.md)|The enum type passed as an argument to the `HasFlag` method call is different from the calling enum type.|
5454
|[CA2249: Consider using String.Contains instead of String.IndexOf](ca2249.md)|Calls to `string.IndexOf` where the result is used to check for the presence or absence of a substring can be replaced by `string.Contains`.|
5555
|[CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has.|
56+
|[CA2251: Use `String.Equals` over `String.Compare`](ca2251.md)|It is both clearer and likely faster to use `String.Equals` instead of comparing the result of `String.Compare` to zero.|

docs/fundamentals/toc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,8 @@ items:
12311231
href: code-analysis/quality-rules/ca2249.md
12321232
- name: CA2250
12331233
href: code-analysis/quality-rules/ca2250.md
1234+
- name: CA2251
1235+
href: code-analysis/quality-rules/ca2251.md
12341236
- name: Code style rules
12351237
items:
12361238
- name: Overview

0 commit comments

Comments
 (0)