All rules
CA1872Performance Enabled by default: As suggestion

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

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

Microsoft docs

Description

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

Cause

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

How to fix violations

To fix a violation of this rule, replace the call chain with either System.Convert.ToHexString or System.Convert.ToHexStringLower.

Example

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();
    }
}

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);
    }
}

When to suppress

It is safe to suppress a warning from this rule; however, we recommend that you use either System.Convert.ToHexString or System.Convert.ToHexStringLower.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0