Home >Backend Development >C++ >String vs. string in C#: When Should You Use Which?
String and String
In C#, when processing text data, you may encounter two similar keywords: "String" and "String". Although they seem to be able to swap, there are subtle differences between them, and there are some suggestions in use.
Definition
"String" is the alias of the System.string class, which represents a series of unicode characters. Its behavior is similar to objects, can be assigned to variables, stores in the collection, and applies its methods and attributes.
"String" (uppercase) indicates the System.string itself. It is not an aliases, usually used in special reference classes rather than in the case.Usage Guide
Although it is technically equivalent, their use has some recommended guidelines:
For the object reference, use "String":
When processing the string as an object, the aliases should be used "String". For example:For class references, use "String":
If you need to reference the System.String class, you should use the capital form "string". For example:<code class="language-csharp">string greeting = "Hello, C#!";</code>
Microsoft style guide
<code class="language-csharp">Console.WriteLine(String.Format("Formatted string: {0}", greeting));</code>Microsoft's encoding guide used to use "string" to reference the class, and use "String" for the object reference. However, these guidelines later developed to encourage as much as possible to use C# aliases as much as possible.
StyleCop(一种编码样式分析器)在现代 C# 编码实践中强制使用 C# 别名。因此,优选使用“string”而不是“String”。
The above is the detailed content of String vs. string in C#: When Should You Use Which?. For more information, please follow other related articles on the PHP Chinese website!