SA1122: Use string.Empty for empty strings
Cause
The C# code includes an empty string, written as "".
Rule description
A violation of this rule occurs when the code contains an empty string. For example:
string s = "";
This will cause the compiler to embed an empty string into the compiled code. Rather than including a hard-coded empty string, use the static string.Empty property:
string s = string.Empty;
Every form of empty string literal is reported, including verbatim, interpolated and raw ones:
string s1 = @"";
string s2 = $"";
string s3 = @$"";
string s4 = """
""";
string s5 = $"""
""";
An interpolated string is only reported when it has no content at all, so $"{value}" and $"text" are not
reported. An empty interpolated string is also not reported when it is converted to FormattableString,
IFormattable or an interpolated string handler type, because string.Empty can't be converted to those types.
A UTF-8 string literal such as ""u8 is not reported, because it is a ReadOnlySpan<byte> rather than a string.
A violation does not occur where the language requires a constant, whichever form it is written in, because
string.Empty is a static read-only field rather than a constant and cannot be used in its place. That covers
attribute arguments, case labels, constant patterns, default parameter values, and const fields and locals.
How to fix violations
To fix a violation of this rule, replace the hard-coded empty string with string.Empty.
How to suppress violations
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1122:UseStringEmptyForEmptyStrings", Justification = "Reviewed.")]
#pragma warning disable SA1122 // UseStringEmptyForEmptyStrings
#pragma warning restore SA1122 // UseStringEmptyForEmptyStrings