这两者之间有什么区别,我应该使用哪一个?

string s = "Hello world!";
String s = "Hello world!";

当前回答

坦率地说,在实践中通常没有System.String和 string之间的区别。

所有类型在 C# 是对象,所有衍生于 System.Object 类. 一个区别是,字符串是一个 C# 关键字,字符串可以用作变量名称. System.String 是这个类型的常规.NET 名称,字符串是方便的 C# 名称. 这里是一个简单的程序,显示 System.String 和字符串之间的区别。

string a = new string(new char[] { 'x', 'y', 'z' });
string b = new String(new char[] { 'x', 'y', 'z' });
String c = new string(new char[] { 'x', 'y', 'z' });
String d = new String(new char[] { 'x', 'y', 'z' });
MessageBox.Show((a.GetType() == typeof(String) && a.GetType() == typeof(string)).ToString()); // shows true
MessageBox.Show((b.GetType() == typeof(String) && b.GetType() == typeof(string)).ToString()); // shows true
MessageBox.Show((c.GetType() == typeof(String) && c.GetType() == typeof(string)).ToString()); // shows true
MessageBox.Show((d.GetType() == typeof(String) && d.GetType() == typeof(string)).ToString()); // shows true

@JonSkeet 在我的编辑器中

public enum Foo : UInt32 { }

我是Visual Studio 2015社区。

其他回答

string 是 System.String 的 C# 中的一个 alias. 所以技术上,没有区别. 它就像 int vs. System.Int32.

至于指导方针,一般建议在您提到对象时使用字符串。

吉。

string place = "world";

同样,我认为一般建议使用 String 如果你需要具体提到课堂。

吉。

string greet = String.Format("Hello {0}!", place);

这是微软在其例子中使用的风格。

似乎该领域的指导方针可能已经改变了,因为StyleCop现在强制使用C#特定的联盟。

基本上,在 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)等。

这个YouTube视频实际上展示了它们的差异。

当我们谈论.NET 时,有两个不同的东西,一个是.NET 框架,另一个是使用该框架的语言(C#,VB.NET 等)。

此分類上一篇

String s = "I am String";

string s = "I am String";

同样,还有其他 C# 数据类型的联盟,如下所示:

现在,从程序员的观点来看,百万美元的问题:那么什么时候使用“String”和“String”?

在下面的代码中,左侧是变量声明,它被声明使用“丝带”。在右侧,我们称一种方法,所以“丝带”更敏感。

string s = String.ToUpper() ;

字符串是关键词,你不能用字符串作为识别器。

条纹不是一个关键词,你可以用它作为一个标识:

例子

string String = "I am a string";

关键词行是 System.String 与关键词问题相同的标志,两者都是相同的。

 typeof(string) == typeof(String) == typeof(System.String)