diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2251.md b/docs/fundamentals/code-analysis/quality-rules/ca2251.md new file mode 100644 index 0000000000000..74a04c458ab84 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/ca2251.md @@ -0,0 +1,40 @@ +--- +title: "CA2251: Use `String.Equals` over `String.Compare`" +description: "Learn about code analysis rule CA2251: Use `String.Equals` over `String.Compare`" +ms.date: 05/31/2021 +ms.topic: reference +f1_keywords: +- CA2251 +- UseStringEqualsOverStringCompare +helpviewer_keywords: +- UseStringEqualsOverStringCompare +- CA2251 +author: NewellClark +--- +# CA2251: Use `String.Equals` over `String.Compare` + +| | Value | +|-|-| +| **Rule ID** |CA2251| +| **Category** |[Usage](usage-warnings.md)| +| **Fix is breaking or non-breaking** |Non-breaking| + +## Cause + +The result of a call to is compared to zero. + +## Rule description + + 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 . + +## How to fix violations + +To fix violations of this rule, replace the expression comparing the result of with a call to . + +## When to suppress warnings + +It is safe to suppress warnings from this rule. + +## See also + +- [Performance warnings](performance-warnings.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 608c753657e93..38794fd791887 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -181,6 +181,7 @@ The following table lists code quality analysis rules. > | [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. | > | [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`. | > | [CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has. | +> | [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. | > | [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. | > | [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. | > | [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. | diff --git a/docs/fundamentals/code-analysis/quality-rules/usage-warnings.md b/docs/fundamentals/code-analysis/quality-rules/usage-warnings.md index dae64fc3a3ff1..694bfc598b07d 100644 --- a/docs/fundamentals/code-analysis/quality-rules/usage-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/usage-warnings.md @@ -53,3 +53,4 @@ Usage rules support proper usage of .NET. |[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.| |[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`.| |[CA2250: Use `ThrowIfCancellationRequested`](ca2250.md) | `ThrowIfCancellationRequested` automatically checks whether the token has been canceled, and throws an `OperationCanceledException` if it has.| +|[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.| diff --git a/docs/fundamentals/toc.yml b/docs/fundamentals/toc.yml index a787c64d69335..31a7c8135a1ae 100644 --- a/docs/fundamentals/toc.yml +++ b/docs/fundamentals/toc.yml @@ -1231,6 +1231,8 @@ items: href: code-analysis/quality-rules/ca2249.md - name: CA2250 href: code-analysis/quality-rules/ca2250.md + - name: CA2251 + href: code-analysis/quality-rules/ca2251.md - name: Code style rules items: - name: Overview