-
Notifications
You must be signed in to change notification settings - Fork 11
StringBuilder
Suggests replacing expressions with equivalent expressions that are more readable or improving performance. The analyzer doesn't make any suggestions if the current language version doesn't support the replacement or the target framework doesn't contain the target method (or method overload).
In the following examples the text
represents any string expression.
StringBuilder Append(char value, int repeatCount)
StringBuilder Append(char[]? value)
StringBuilder Append(char[]? value, int startIndex, int charCount)
StringBuilder Append(string? value)
StringBuilder Append(string? value, int startIndex, int count)
StringBuilder Append(StringBuilder? value)
StringBuilder Append(StringBuilder? value, int startIndex, int count)
StringBuilder Append(object? value)
Case | Suggested replacement | When suggested |
---|---|---|
builder.Append(null) |
builder |
value is null
|
builder.Append(c, 0) |
builder |
repeatCount is a 0 |
builder.Append(c, 1) |
builder.Append(c) |
repeatCount is a 1 |
builder.Append([]) |
builder |
value is an empty array |
builder.Append(null, 0, 0) |
builder |
value is null , startIndex and charCount are both 0 |
builder.Append([], 0, 0) |
builder |
value is an empty array, startIndex and charCount are both 0 |
builder.Append("") |
builder |
value is an empty string |
builder.Append("a") |
builder.Append('a') |
value is a one-character constant string |
builder.Append(null, 0, 0) |
builder |
value is null , startIndex and count are both 0 |
builder.Append(s, startIndex, 0) |
builder |
s is not null here, startIndex is a non-negative constant value, and count is 0 |
builder.Append(sb, startIndex, 0) |
builder |
sb is not null here, startIndex is a non-negative constant value, and count is 0 |
builder.Append("abc", 1, 1) |
builder.Append('b') |
s is a constant string, startIndex is constants indicating a valid position within s , and count is 1 |
where c
is a char
value, s
is a string, sb
is a StringBuilder
, and startIndex
and count
are int
values.
StringBuilder AppendJoin(string? separator, params object?[] values)
StringBuilder AppendJoin(string? separator, params ReadOnlySpan<object?> values)
StringBuilder AppendJoin<T>(string? separator, IEnumerable<T> values)
StringBuilder AppendJoin(string? separator, params string?[] values)
StringBuilder AppendJoin(string? separator, params ReadOnlySpan<string?> values)
StringBuilder AppendJoin(char separator, params object?[] values)
StringBuilder AppendJoin(char separator, params ReadOnlySpan<object?> values)
StringBuilder AppendJoin<T>(char separator, IEnumerable<T> values)
StringBuilder AppendJoin(char separator, params string?[] values)
StringBuilder AppendJoin(char separator, params ReadOnlySpan<string?> values)
Case | Suggested replacement | When suggested |
---|---|---|
builder.AppendJoin(separator, []) |
builder |
values is an empty array, collection, or span expression |
builder.AppendJoin(separator, [item]) |
builder.Append(item) |
values consists of a single item |
builder.AppendJoin(",", values) |
builder.AppendJoin(',', values) |
separator a one-character constant string |
where separator
is a string or a char
value, values
is an array, an IEnumerable<T>
, ReadOnlySpan<object?>
, or ReadOnlySpan<string?>
.
StringBuilder Insert(int index, string? value, int count)
StringBuilder Insert(int index, string? value)
StringBuilder Insert(int index, object? value)
Case | Suggested replacement | When suggested |
---|---|---|
builder.Insert(index, s, 1) |
builder.Insert(index, s) |
count is 1 |
builder.Insert(index, "a", 1) |
builder.Insert(index, 'a') |
value is a one-character constant string and count is 1 |
builder.Insert(index, "a") |
builder.Insert(index, 'a') |
value is a one-character constant string |
builder.Insert(index, null) |
builder |
value is null
|
where index
is an int
value, s
is a string, and count
is an int
value.
StringBuilder Replace(char oldChar, char newChar)
StringBuilder Replace(string oldValue, string? newValue)
StringBuilder Replace(string oldValue, string? newValue, int startIndex, int count)
Case | Suggested replacement | When suggested |
---|---|---|
builder.Replace('a', 'b') |
builder.Replace('a', 'b') |
oldValue and newValue are one-character constant strings |
builder.Replace("abc", "abc") |
builder |
oldValue is a constant non-empty string and newValue is a constant string indentical to oldValue
|
builder.Replace("a", "b") |
builder.Replace('a', 'b') |
oldValue and newValue are one-character constant strings |
builder.Replace("a", "b", startIndex, count) |
builder.Replace('a', 'b', , startIndex, count) |
oldValue and newValue are one-character constant strings |
where startIndex
and count
are int
values.
💡 Quick-fixes are available.
💡 The analyzers can be deactivated in the ReSharper Options dialog.