-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.adegeo:dotnet/docs into adegeo-39263-ub…
…untu
- Loading branch information
Showing
8 changed files
with
175 additions
and
29 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
docs/fundamentals/code-analysis/quality-rules/ca1872.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
--- | ||
title: "CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString' (code analysis)" | ||
description: "Learn about code analyzer rule CA1872 - Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'" | ||
ms.date: 03/21/2024 | ||
ms.topic: reference | ||
f1_keywords: | ||
- CA1872 | ||
- PreferConvertToHexStringOverBitConverterAnalyzer | ||
helpviewer_keywords: | ||
- CA1872 | ||
author: mpidash | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
|
||
# CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString' | ||
|
||
| | Value | | ||
| ----------------------------------- |---------------------------------------------------------------------------------------------------------------| | ||
| **Rule ID** | CA1872 | | ||
| **Title** | Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString' | | ||
| **Category** | [Performance](performance-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-breaking | | ||
| **Enabled by default in .NET 9** | As suggestion | | ||
|
||
## Cause | ||
|
||
A call to <xref:System.BitConverter.ToString%2A?displayProperty=nameWithType> followed by a call to <xref:System.String.Replace%2A?displayProperty=nameWithType> to remove dashes is used to encode bytes to a hexadecimal string representation. | ||
This rule also fires if <xref:System.String.ToLower%2A?displayProperty=nameWithType> is used in the call chain. | ||
|
||
## Rule description | ||
|
||
Use <xref:System.Convert.ToHexString%2A?displayProperty=nameWithType> or <xref:System.Convert.ToHexStringLower%2A?displayProperty=nameWithType> when encoding bytes to a hexadecimal string representation. These methods are more efficient and allocation-friendly than using <xref:System.BitConverter.ToString%2A?displayProperty=nameWithType> in combination with <xref:System.String.Replace%2A?displayProperty=nameWithType> to remove dashes and <xref:System.String.ToLower%2A?displayProperty=nameWithType>. | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, replace the call chain with either <xref:System.Convert.ToHexString%2A?displayProperty=nameWithType> or <xref:System.Convert.ToHexStringLower%2A?displayProperty=nameWithType>. | ||
|
||
## Example | ||
|
||
The following code snippet shows a violation of CA1872: | ||
|
||
```csharp | ||
using System; | ||
using System.Text; | ||
|
||
class HelloWorldEncoder | ||
{ | ||
private readonly byte[] _data = Encoding.ASCII.GetBytes("Hello World"); | ||
|
||
public string Encode() | ||
{ | ||
return BitConverter.ToString(_data).Replace("-", ""); | ||
} | ||
|
||
public string EncodeToLower() | ||
{ | ||
return BitConverter.ToString(_data).Replace("-", "").ToLower(); | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Imports System | ||
Imports System.Text | ||
|
||
Class HelloWorldEncoder | ||
Private ReadOnly _data As Byte() = Encoding.ASCII.GetBytes("Hello World") | ||
|
||
Public Function Encode() As String | ||
Return BitConverter.ToString(_data).Replace("-", "") | ||
End Function | ||
|
||
Public Function EncodeToLower() As String | ||
Return BitConverter.ToString(_data).Replace("-", "").ToLower() | ||
End Function | ||
End Class | ||
``` | ||
|
||
The following code snippet fixes the violation: | ||
|
||
```csharp | ||
using System; | ||
using System.Text; | ||
|
||
class HelloWorldEncoder | ||
{ | ||
private readonly byte[] _data = Encoding.ASCII.GetBytes("Hello World"); | ||
|
||
public string Encode() | ||
{ | ||
return Convert.ToHexString(data); | ||
} | ||
|
||
public string EncodeToLower() | ||
{ | ||
return Convert.ToHexStringLower(data); | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Imports System | ||
Imports System.Text | ||
|
||
Class HelloWorldEncoder | ||
Private ReadOnly _data As Byte() = Encoding.ASCII.GetBytes("Hello World") | ||
|
||
Public Function Encode() As String | ||
Return Convert.ToHexString(data) | ||
End Function | ||
|
||
Public Function EncodeToLower() As String | ||
Return Convert.ToHexStringLower(data) | ||
End Function | ||
End Class | ||
``` | ||
|
||
## When to suppress warnings | ||
|
||
It is safe to suppress a warning from this rule; however, we recommend that you use either <xref:System.Convert.ToHexString%2A?displayProperty=nameWithType> or <xref:System.Convert.ToHexStringLower%2A?displayProperty=nameWithType>. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA1872 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1872 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA1872.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters