这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
有一个实用的区别在线和线之间。
nameof(String); // compiles
nameof(string); // doesn't compile
這是因為字符串是一個關鍵字(在這種情況下,一個名稱),而字符串是一個類型。
同样的情况也适用于其他联盟。
| Alias | Type |
|-----------|------------------|
| bool | System.Boolean |
| byte | System.Byte |
| sbyte | System.SByte |
| char | System.Char |
| decimal | System.Decimal |
| double | System.Double |
| float | System.Single |
| int | System.Int32 |
| uint | System.UInt32 |
| long | System.Int64 |
| ulong | System.UInt64 |
| object | System.Object |
| short | System.Int16 |
| ushort | System.UInt16 |
| string | System.String |
其他回答
字符串等于 System.String 在 VS2015 如果您写下此:
System.String str;
如何编辑器将显示潜在的修复,以优化它,并在应用该修复后,它将看起来像这个
string str;
使用系统类型使您更容易在 C# 和 VB.Net 之间端口,如果您进入这种类型的东西。
您不需要进口名称空间(使用系统;)来使用字符串,因为它是一个全球的名称 System.String。
要了解更多关于 Alias 的信息,您可以查看此链接。
事实上,每个 C# 类型都有一个相当于.NET. 作为另一个例子,在 C# 地图中简短和 int 到 Int16 和 Int32 在.NET. 因此,技术上没有线和线之间的区别,但在 C# 中,线是.NET 框架中的线类的标志。
线条:一个线条对象被称为不可改变(仅阅读),因为它的值一旦创建,就无法修改。 似乎改变一个线条对象的方法实际上返回一个包含修改的新线条对象。
字符串: 字符串类型代表一个序列的零或多个 Unicode 字符. 字符串是字符串在.NET 框架中的字符串的标志. 字符串是内在的 C# 数据类型,是系统提供的类型“System.String”的标志。
String 与 String 的区别: