|
| 1 | +--- |
| 2 | +title: "CA1834: Use StringBuilder.Append(char) for single character strings" |
| 3 | +ms.date: 08/04/2020 |
| 4 | +ms.topic: reference |
| 5 | +f1_keywords: |
| 6 | + - "UseStringBuilderAppendChar" |
| 7 | + - "CA1834" |
| 8 | +helpviewer_keywords: |
| 9 | + - "UseStringBuilderAppendChar" |
| 10 | + - "CA1834" |
| 11 | +author: pgovind |
| 12 | +ms.author: prgovi |
| 13 | +manager: jeffhand |
| 14 | +ms.workload: |
| 15 | + - "multiple" |
| 16 | +--- |
| 17 | +# CA1834: Use StringBuilder.Append(char) for single character strings |
| 18 | + |
| 19 | +|Item|Value| |
| 20 | +|-|-| |
| 21 | +|CheckId|CA1834| |
| 22 | +|Category|Microsoft.Performance| |
| 23 | +|Breaking change|Non-breaking| |
| 24 | + |
| 25 | +## Cause |
| 26 | + |
| 27 | +This rule fires when a unit length string is passed to the <xref:System.Text.StringBuilder.Append%2A> method. |
| 28 | + |
| 29 | +## Rule description |
| 30 | + |
| 31 | +When calling `StringBuilder.Append` with a unit length string, consider using a `const char` rather than a unit length `const string` to improve performance. |
| 32 | + |
| 33 | +## How to fix violations |
| 34 | + |
| 35 | +The violation can either be fixed manually, or, in some cases, using Quick Actions to fix code in Visual Studio. Examples: |
| 36 | + |
| 37 | +##### Example 1 |
| 38 | +Invocations of `StringBuilder.Append` with a string literal of unit length: |
| 39 | +```csharp |
| 40 | +using System; |
| 41 | +using System.Text; |
| 42 | + |
| 43 | +namespace TestNamespace |
| 44 | +{ |
| 45 | + class TestClass |
| 46 | + { |
| 47 | + private void TestMethod() |
| 48 | + { |
| 49 | + StringBuilder sb = new StringBuilder(); |
| 50 | + sb.Append("a"); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | +> [!TIP] |
| 56 | +> A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press **Ctrl**+**.** (period). Choose **Consider using 'StringBuilder.Append(char)' when applicable.** from the list of options that is presented. |
| 57 | +> |
| 58 | +>  |
| 59 | +
|
| 60 | +##### Fix applied by Visual Studio |
| 61 | +```csharp |
| 62 | +using System; |
| 63 | +using System.Text; |
| 64 | + |
| 65 | +namespace TestNamespace |
| 66 | +{ |
| 67 | + class TestClass |
| 68 | + { |
| 69 | + private void TestMethod() |
| 70 | + { |
| 71 | + StringBuilder sb = new StringBuilder(); |
| 72 | + sb.Append('a'); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +In some cases, for example when using a unit length `const string` class field, a code-fix is not suggested by Visual Studio (but the analyzer still fires). These instances require a manual fix. |
| 79 | + |
| 80 | +##### Example 2 |
| 81 | +Invocations of `StringBuilder.Append` with a `const string` class field of unit length: |
| 82 | +```cs |
| 83 | +using System; |
| 84 | +using System.Text; |
| 85 | + |
| 86 | +namespace TestNamespace |
| 87 | +{ |
| 88 | + public class Program |
| 89 | + { |
| 90 | + public const string unitString = "a"; |
| 91 | + |
| 92 | + static void Main(string[] args) |
| 93 | + { |
| 94 | + StringBuilder sb = new StringBuilder(); |
| 95 | + sb.Append(unitString); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | +After careful analysis, `unitString` here can be changed to a `char` without causing any build errors. |
| 101 | + |
| 102 | +##### Fix |
| 103 | +```cs |
| 104 | +using System; |
| 105 | +using System.Text; |
| 106 | + |
| 107 | +namespace TestNamespace |
| 108 | +{ |
| 109 | + public class Program |
| 110 | + { |
| 111 | + public const char unitString = 'a'; |
| 112 | + |
| 113 | + static void Main(string[] args) |
| 114 | + { |
| 115 | + StringBuilder sb = new StringBuilder(); |
| 116 | + sb.Append(unitString); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## When to suppress warnings |
| 123 | + |
| 124 | +It's safe to suppress a violation of this rule if you're not concerned about improving performance when using `StringBuilder`. |
| 125 | + |
| 126 | +## See also |
| 127 | + |
| 128 | +- [Performance warnings](../code-quality/performance-warnings.md) |
0 commit comments