-
Notifications
You must be signed in to change notification settings - Fork 222
/
S3063_c#.html
37 lines (36 loc) · 1.13 KB
/
S3063_c#.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<p><code>StringBuilder</code> instances that are <code>append</code>ed but never <code>toString</code>ed needlessly clutter the code, and worse are a
drag on performance. Either they should be removed, or the missing <code>toString</code> call added.</p>
<h2>Noncompliant Code Example</h2>
<pre>
public void doSomething(List<string> strings) {
StringBuilder sb = new StringBuilder(); // Noncompliant
sb.Append("Got: ");
foreach(string str in strings) {
sb.Append(str).Append(", ");
// ...
}
}
</pre>
<h2>Compliant Solution</h2>
<pre>
public void doSomething(List<string> strings) {
foreach(string str in strings) {
// ...
}
}
</pre>
<p>or</p>
<pre>
public void doSomething(List<string> strings) {
StringBuilder sb = new StringBuilder(); // Noncompliant
sb.Append("Got: ");
foreach(string str in strings) {
sb.Append(str).Append(", ");
// ...
}
_logger.LogInformation(sb.toString, DateTimeOffset.UtcNow);
}
</pre>
<h2>Exceptions</h2>
<p>This rule ignores <code>StringBuilder</code>s that are passed as method arguments on the grounds that they are likely <code>toString</code>ed
there.</p>