This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
| Value | |
|---|---|
| Rule ID | CA1872 |
| Title | PreferConvert.ToHexString andConvert.ToHexStringLower over call chains based onBitConverter.ToString |
| Category | Performance |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default in .NET 10 | As suggestion |
A call toBitConverter.ToString followed by a call toString.Replace to remove dashes is used to encode bytes to a hexadecimal string representation.This rule also fires ifString.ToLower is used in the call chain.
UseConvert.ToHexString orConvert.ToHexStringLower when encoding bytes to a hexadecimal string representation. These methods are more efficient and allocation-friendly than usingBitConverter.ToString in combination withString.Replace to remove dashes andString.ToLower.
To fix a violation of this rule, replace the call chain with eitherConvert.ToHexString orConvert.ToHexStringLower.
The following code snippet shows a violation of CA1872:
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(); }}Imports SystemImports System.TextClass 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 FunctionEnd ClassThe following code snippet fixes the violation:
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); }}Imports SystemImports System.TextClass 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 FunctionEnd ClassIt is safe to suppress a warning from this rule; however, we recommend that you use eitherConvert.ToHexString orConvert.ToHexStringLower.
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1872// The code that's violating the rule is on this line.#pragma warning restore CA1872To disable the rule for a file, folder, or project, set its severity tonone in theconfiguration file.
[*.{cs,vb}]dotnet_diagnostic.CA1872.severity = noneFor more information, seeHow to suppress code analysis warnings.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?