String代表System.String,它是一个. NET Framework类型。#2是一个别名在C#语言中代表System.String。它们都编译为#1在IL(中间语言),所以没有区别。选择你喜欢的并使用它。如果你用C#编码,我更喜欢string,因为它是C#类型别名,并且被C#程序员所熟知。
BinaryReader br = new BinaryReader(...);float val = br.ReadSingle(); // OK, but feels unnaturalSingle val = br.ReadSingle(); // OK and feels good
Many programmers that use C# exclusively tend to forget that other programminglanguages can be used against the CLR, and because of this, C#-isms creep into theclass library code. For example, Microsoft’s FCL is almost exclusively written in C# anddevelopers on the FCL team have now introduced methods into the library such asArray’s GetLongLength, which returns an Int64 value that is a long in C# but notin other languages (like C++/CLI). Another example is System.Linq.Enumerable’sLongCount method.
I didn't get his opinion before I read the complete paragraph.
string:字符串类型表示零个或多个Unicode字符的序列。string是. NET Framework中String的别名。string是固有的C#数据类型,是系统提供的类型“System. String”的别名。C#规范指出,作为样式问题,关键字(字符串)优先于完整的系统类型名称(System. String或String)。尽管string是一种引用类型,但定义相等运算符(==和!=)是为了比较字符串对象的值,而不是引用。这使得字符串相等的测试更加直观。例如:
标识符String在C#中没有具体含义。它是一个标识符,可以通过所有名称查找规则,如Widget、Student等……它可以绑定到字符串,也可以绑定到另一个程序集中的类型,其目的可能与string完全不同。更糟糕的是,它可以被定义为像String s = "hello";这样的代码继续编译。
class TricksterString {void Example() {String s = "Hello World"; // Okay but probably not what you expect.}}
class String {public static implicit operator String(string s) => null;}