All rules
CA1830Performance Enabled by default: As suggestion

Prefer strongly-typed Append and Insert method overloads on StringBuilder

Prefer strongly-typed Append and Insert method overloads on StringBuilder

Microsoft docs

Description

System.Text.StringBuilder.Append and System.Text.StringBuilder.Insert provide overloads for multiple types beyond System.String. When possible, prefer the strongly-typed overloads over using ToString() and the string-based overload.

Cause

An System.Text.StringBuilder Append or Insert method was called with an argument that was the result of calling ToString on a type for which the Append or Insert method has a dedicated overload.

How to fix violations

Delete the unnecessary ToString() from the invocation.

Example

using System.Text;

class C
{
    int _value;

    // Violation
    public void Log(StringBuilder destination)
    {
        destination.Append("Value: ").Append(_value.ToString()).AppendLine();
    }

    // Fixed
    public void Log(StringBuilder destination)
    {
        destination.Append("Value: ").Append(_value).AppendLine();
    }
}

When to suppress

It's safe to suppress a violation of this rule if you're not concerned about the performance impact from unnecessary string allocations.

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