这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
这两者之间有什么区别,我应该使用哪一个?
string s = "Hello world!";
String s = "Hello world!";
当前回答
如果有用的是真正看到没有线和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 ]
其他回答
.NET 类型与其他对象类型相同的颜色(值类型是合适的对象,毕竟)。
条件和控制的关键词(如如果,交换和返回)是下层和彩色黑蓝色(默认情况下)。
考虑一下:
String someString;
string anotherString;
基本上,在 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()方法。
String 是 System.String 的意思,它是.NET Framework 类型. String 是 System.String 的 C# 语言中的一个字符串. 两者都编写到 System.String 在 IL (中间语言),所以没有区别. 选择你喜欢什么,并使用它. 如果你在 C# 中编码,我会更喜欢字符串,因为它是 C# 类型的字符串和由 C# 程序员熟悉。
我可以说同样的事情(int,System.Int32)等。
字符串是关键词,你不能用字符串作为识别器。
条纹不是一个关键词,你可以用它作为一个标识:
例子
string String = "I am a string";
关键词行是 System.String 与关键词问题相同的标志,两者都是相同的。
typeof(string) == typeof(String) == typeof(System.String)
Jeffrey Richter 写道:
另一种方法是C#编译器自动假设您在所有源代码文件中使用下列指南:
using int = System.Int32;
using uint = System.UInt32;
using string = System.String;
...
我已经看到一些开发人员混淆了,不知道是否在他们的代码中使用线条或线条. 因为在C#线条(一个关键词)地图准确到System.String(一个FCL类型),没有区别,也可以使用。