אירוע
בניית אפליקציות וסוכנים של בינה מלאכותית
17 במרץ, 21 - 21 במרץ, 10
הצטרף לסידרה של פגישות כדי לבנות פתרונות מדרגיים של בינה מלאכותית בהתבסס על מקרי שימוש מהעולם האמיתי עם מפתחים ומומחים אחרים.
הירשם עכשיוהדפדפן הזה אינו נתמך עוד.
שדרג ל- Microsoft Edge כדי לנצל את התכונות, עדכוני האבטחה והתמיכה הטכנית העדכניים ביותר.
Property | Value |
---|---|
Rule ID | CA1834 |
Title | Use StringBuilder.Append(char) for single character strings |
Category | Performance |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | As suggestion |
This rule fires when a unit length string is passed to the Append method.
When calling StringBuilder.Append
with a unit length string, consider using a const char
rather than a unit length const string
to improve performance.
The violation can either be fixed manually, or, in some cases, using Quick Actions to fix code in Visual Studio. Examples:
Invocations of StringBuilder.Append
with a string literal of unit length:
using System;
using System.Text;
namespace TestNamespace
{
class TestClass
{
private void TestMethod()
{
StringBuilder sb = new StringBuilder();
sb.Append("a");
}
}
}
עצה
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.
Fix applied by Visual Studio:
using System;
using System.Text;
namespace TestNamespace
{
class TestClass
{
private void TestMethod()
{
StringBuilder sb = new StringBuilder();
sb.Append('a');
}
}
}
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.
Invocations of StringBuilder.Append
with a const string
class field of unit length:
using System;
using System.Text;
namespace TestNamespace
{
public class Program
{
public const string unitString = "a";
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append(unitString);
}
}
}
After careful analysis, unitString
here can be changed to a char
without causing any build errors.
using System;
using System.Text;
namespace TestNamespace
{
public class Program
{
public const char unitString = 'a';
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append(unitString);
}
}
}
It's safe to suppress a violation of this rule if you're not concerned about improving performance when using StringBuilder
.
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1834
// The code that's violating the rule is on this line.
#pragma warning restore CA1834
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1834.severity = none
For more information, see How to suppress code analysis warnings.
משוב של .NET
.NET הוא פרויקט קוד פתוח. בחר קישור כדי לספק משוב:
אירוע
בניית אפליקציות וסוכנים של בינה מלאכותית
17 במרץ, 21 - 21 במרץ, 10
הצטרף לסידרה של פגישות כדי לבנות פתרונות מדרגיים של בינה מלאכותית בהתבסס על מקרי שימוש מהעולם האמיתי עם מפתחים ומומחים אחרים.
הירשם עכשיו