这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
有一个区别 - 您不能使用 String 没有使用系统; 提前。
其他回答
另一个没有提到的论点,最好是复活节案例:
System.String 是参考类型,参考类型名称是经典案例。
有一个实用的区别在线和线之间。
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 |
两者都是相同的,区别在于你如何使用它。
字符串为变量
String 是为了召唤其他 String 类方法
如:
string fName = "John";
string lName = "Smith";
string fullName = String.Concat(fName,lName);
if (String.IsNullOrEmpty(fName))
{
Console.WriteLine("Enter first name");
}
是的,这不是他们之间的区别,就像博尔和布莱恩一样。
基本上,在 C# 中,线和线之间没有区别。
String 是 System.String 中的.NET 框架中的一个类,而较低的案例字符串则是 System.String 的标志。
登录两个类型的完整名称可以证明这一点
string s1= "hello there 1";
String s2 = "hello there 2";
Console.WriteLine(s1.GetType().FullName); // System.String
Console.WriteLine(s2.GetType().FullName); // System.String
大多数开发人员使用字符串在C#中声明变量,并使用System.String类使用任何内置的字符串方法,例如,String.IsNullOrEmpty()方法。