Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'

Feedback

In this article

Value
Rule IDCA1872
TitlePreferConvert.ToHexString andConvert.ToHexStringLower over call chains based onBitConverter.ToString
CategoryPerformance
Fix is breaking or non-breakingNon-breaking
Enabled by default in .NET 10As suggestion

Cause

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.

Rule description

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.

How to fix violations

To fix a violation of this rule, replace the call chain with eitherConvert.ToHexString orConvert.ToHexStringLower.

Example

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 Class

The 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 Class

When to suppress warnings

It is safe to suppress a warning from this rule; however, we recommend that you use eitherConvert.ToHexString orConvert.ToHexStringLower.

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.

#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 tonone in theconfiguration file.

[*.{cs,vb}]dotnet_diagnostic.CA1872.severity = none

For more information, seeHow to suppress code analysis warnings.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?