-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use StringBuilder.Append(char) for single character strings #33786
Comments
Estimates:
|
Quick question about a corner case here: Consider the following code: const string aa = "a", bb = "b";
sb.Append(aa); //sb is a StringBuilder The analyzer would detect
const char aa = "a";
const string bb = "b";
sb.Append(aa); //sb is a StringBuilder OR Which do we want? P.S: There is also the more straightforward case when we have: const string aa = "a", bb = "b";
sb.Append(aa).Append(bb); //sb is a StringBuilder where the code-fix is more obvious and just results in: const char aa = 'a', bb = 'b'; |
I recommend that we just bail on multiple declarations instead of trying to get clever. |
There are only 2 real corner cases to consider in this Analyzer:
1 issue to consider in the code-fix is that it can lead to build breaks in some cases. For ex: // Assume we have the following method declaration
public string AMethod(string foo)
{
}
// Somewhere later we have
const string str = "a";
stringBuilder.Append(str); // This will tag the analyzer and suggest a code-fix
AMethod(str); // If the code-fix was invoked, this will now be a compiler error since str will now be a char. |
It's common to see calls to
StringBuilder.Append(string)
with a conststring
containing a single character, e.g.","
. These would be slightly cheaper as calls using a constchar
instead.Category: Performance
The text was updated successfully, but these errors were encountered: