What is the difference between String and string i

2018-12-31 08:17发布

Example (note the case):

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

What are the guidelines for the use of each? And what are the differences?

30条回答
步步皆殇っ
2楼-- · 2018-12-31 09:21

Both are same. But from coding guidelines perspective it's better to use string instead of String. This is what generally developers use. e.g. instead of using Int32 we use int as int is alias to Int32

FYI “The keyword string is simply an alias for the predefined class System.String.” - C# Language Specification 4.2.3 http://msdn2.microsoft.com/En-US/library/aa691153.aspx

查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 09:21

Yes, that's no difference between them, just like the bool and Boolean.

查看更多
其实,你不懂
4楼-- · 2018-12-31 09:22

It's been covered above; however, you can't use string in reflection; you must use String.

查看更多
与君花间醉酒
5楼-- · 2018-12-31 09:23

Coming late to the party: I use the CLR types 100% of the time (well, except if forced to use the C# type, but I don't remember when the last time that was).

I originally started doing this years ago, as per the CLR books by Ritchie. It made sense to me that all CLR languages ultimately have to be able to support the set of CLR types, so using the CLR types yourself provided clearer, and possibly more "reusable" code.

Now that I've been doing it for years, it's a habit and I like the coloration that VS shows for the CLR types.

The only real downer is that auto-complete uses the C# type, so I end up re-typing automatically generated types to specify the CLR type instead.

Also, now, when I see "int" or "string", it just looks really wrong to me, like I'm looking at 1970's C code.

查看更多
像晚风撩人
6楼-- · 2018-12-31 09:24

There is no difference.

The C# keyword string maps to the .NET type System.String - it is an alias that keeps to the naming conventions of the language.

Similarly, int maps to System.Int32.

查看更多
时光乱了年华
7楼-- · 2018-12-31 09:24

string is a keyword, and you can't use string as an identifier.

String is not a keyword, and you can use it as an identifier:

Example

string String = "I am a string";

The keyword string is an alias for System.String aside from the keyword issue, the two are exactly equivalent.

 typeof(string) == typeof(String) == typeof(System.String)
查看更多
登录 后发表回答