The difference between "string" and "string" in the c#
In C#, "String" and "String" look different, but what are the differences between them? Let's take a closer look.
The essential equivalent
"String" in C# is the alias of the class, a abbreviation form. From the bottom level, the following code fragments are equivalent:
System.String
Therefore, from a technical point of view, there is no inherent difference between the two. They all point to the same object, similar to the relationship between "int" and "System.int32".
<code class="language-csharp">string s = "Hello world!"; String s = "Hello world!";</code>
Although the technical difference is small, for the clearness and consistency of the code, it is generally recommended to follow some agreements:
Use "string" as an object reference:
When the string object is referenced as a variable, "String" is usually used.Use "string" as a class reference:
If you need to explicitly reference the<code class="language-csharp"> string place = "world";</code>
This method is consistent with the style used by Microsoft in its code example. System.String
<code class="language-csharp"> string greet = String.Format("Hello {0}!", place);</code>
Conclusion
Although "String" and "String" look different, their bottom layer is equivalent because "string" is an alias. By observing the recommendation of the recommended usage, developers can improve the readability and maintenance of the C# code, especially when processing string.
The above is the detailed content of What's the Difference Between 'string' and 'String' in C#?. For more information, please follow other related articles on the PHP Chinese website!