这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
基本上,在 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()方法。
其他回答
正如你所知道的,字符串只是 System.String 的名称,但我应该使用什么?它只是个人偏好。
在我的情况下,我喜欢使用字符串而不是使用 System.String 因为 String 需要使用 System 的名称空间;或者使用 System.String 的全名。
所以我相信 alias string 是为了简单而创建的,我喜欢它!
在 MSDN 文档的背景下, String 类像任何其他数据类型(例如 XmlReader、StreamReader)一样在 BCL 中文档。
字符串被记录为一个关键字(C#参考)或任何基本的C#语言构造(例如,为,同时,默认)。
参考。
字符串是一种序列的字符集,用于代表文本。
序列对象是代表序列的 System.Char 对象的序列集,而 System.Char 对象与 UTF-16 代码单位相匹配。
String 对象的值是 System.Char 对象的序列收藏的内容,这个值是不可改变的(即它仅是阅读的)。
有关线条不变性的更多信息,请参见MSDN中的不变性和StringBuilder类部分。
内存中的 String 对象的最大尺寸为 2GB 或大约 1 亿个字符。
注意 : 答案是从 msdn 帮助部分提取的. 在 msdn String Class 主题中,您可以在此处查看完整内容。
如果有用的是真正看到没有线和System.String之间的区别:
var method1 = typeof(MyClass).GetMethod("TestString1").GetMethodBody().GetILAsByteArray();
var method2 = typeof(MyClass).GetMethod("TestString2").GetMethodBody().GetILAsByteArray();
//...
public string TestString1()
{
string str = "Hello World!";
return str;
}
public string TestString2()
{
String str = "Hello World!";
return str;
}
两者都产生相同的 IL 比特序列:
[ 0, 114, 107, 0, 0, 112, 10, 6, 11, 43, 0, 7, 42 ]
基本上,在 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()方法。