是什么在C#和字符串之间的区别? [英] What is the difference between String and string in C#?
问题描述
示例(请注意大小写的):
string s = "Hello world!";
String S = "Hello world!";
什么是使用每一项准则?什么区别?
What are the guidelines for the use of each? And what are the differences?
推荐答案
字符串
是在C#中 System.String
。因此从技术上讲,没有任何区别。这就像 INT
VS 的 System.Int32
。
string
is an alias in C# for System.String
. So technically, there is no difference. It's like int
vs. System.Int32
.
不过,诠释自动引用 System.Int32
,因为它是默认的,同时也有许多其他的整数( INT
)类型。使用 INT
只是默认为32位整数,如果你鸵鸟政策明确地告诉你code应该期待什么类型的整数。如果引用的Int16
的不是int,你会从指定的默认类型的Int32
。
But int automatically references System.Int32
because it is the default, while there are many other integer(int
) types. Using int
just defaults to the 32 bit integer if you don´t explicitly tell in your code what type of integer it should expect. If you reference Int16
instead of int, you will specify the type from the default Int32
.
至于指引,我认为这是一般建议使用字符串
任何时候你指的对象。
As far as guidelines, I think it's generally recommended to use string
any time you're referring to an object.
例如。
string place = "world";
同样,我认为这是一般建议使用字符串
如果你需要特别提到的类。
Likewise, I think it's generally recommended to use String
if you need to refer specifically to the class.
例如
string greet = String.Format("Hello {0}!", place);
这是微软趋向于使用他们的榜样一风格>。
目前看来,在这方面的指导可能已经改变,因为了StyleCop 现在强制使用C#的具体别名
It appears that the guidance in this area may have changed, as StyleCop now enforces the use of the C# specific aliases.
这篇关于是什么在C#和字符串之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!