Edit

Share via


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
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 9 As suggestion

Cause

A call to BitConverter.ToString followed by a call to String.Replace to remove dashes is used to encode bytes to a hexadecimal string representation. This rule also fires if String.ToLower is used in the call chain.

Rule description

Use Convert.ToHexString or Convert.ToHexStringLower when encoding bytes to a hexadecimal string representation. These methods are more efficient and allocation-friendly than using BitConverter.ToString in combination with String.Replace to remove dashes and String.ToLower.

How to fix violations

To fix a violation of this rule, replace the call chain with either Convert.ToHexString or Convert.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 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:

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 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 Convert.ToHexString or Convert.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 to none in the configuration file.

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

For more information, see How to suppress code analysis warnings.