标签云

微信群

扫码加入我们

WeChat QR Code

Okay, this may be a dumb question, but I've not been able to find any information on it.

Are String.Empty and string.Empty the same? I always find myself gravitating towards using the upper case version (String.Empty) because I prefer the color and look of it in my IDE than the lower case version (string.Empty)...

Is there a "correct" way to use these that differ or is it entirely down to personal preference? It was my assumption that they're both the same, but to be honest, I never gave it any thought until for whatever reason today I wondered "If they both exist, they must both exist for a reason".

Is there a reason that anyone knows of? If so, what is it? Can anyone enlighten me?

P.S. The "exact duplicates" only answer half of the question - "which is right?", not the "why do they both exist?"


Exact Duplicate: What is the difference between String and string in C#?

Exact Duplicate: String vs string in C#


Been asked several times over. Might want to search for common, possibly asked questions such as these before submitting a new one.

2018年08月22日18分20秒

I already checked the list of previously asked questions and didn't come up with those. Thanks for pointing those out though. Very useful.

2018年08月22日18分20秒

P.S. Those "exact duplicates" only answered half of my question - "which is the right way?". The other half of my question was "why do they both exist?", which neither of those "exact duplicates" answers.

2018年08月22日18分20秒

I also find using google as Stackoverflow's search engine to work much better than the SQL full-text search implemented here on the site. Google for, "difference between string and string in c# site:stackoverflow.com". I find much better results with google

2018年08月22日18分20秒

Ooh, good tip, I never thought of that...

2018年08月22日18分20秒

No, he is " gravitating towards using the upper case version (String.Empty)", thus doing it wrong according to your saying...

2018年08月22日18分20秒

Did you mean to say it's best practices NOT to use these aliases?

2018年08月22日18分20秒

Also, I would rather not type Int32 instead of int.

2018年08月22日18分20秒

I tend to use the alias when declaring the type, and the actual name when calling a static member. So int foo, String.Empty and Int32.Parse for me.

2018年08月22日18分20秒

It's also Microsofts recommended programming practices to use aliased versions of the data types.

2018年08月22日18分20秒

hmm, looks like i was too slow :)

2018年08月22日18分20秒

So depending on what you're doing, you may want to use Int32 instead of int if you need exactly 32 bits. I'm thinking that int may not guarantee that it will always be 32bit or else there would be no reason for the keyword.

2018年08月22日18分20秒

styfle: Nah, it does. From the C# spec: • The int type represents signed 32-bit integers with values between –2147483648 and 2147483647. int is an alias for Int32.

2018年08月22日18分20秒

This is not true. string and System.String are the exact same thing: msdn.microsoft.com/en-us/library/ms228362.aspx

2018年08月22日18分20秒

Evan didn't understand what Shalmanese said ... this answer doesn't say they aren't the same, just that String (and string) are references, not value types.

2018年08月22日18分20秒

Just to be clear so not to spread misinformation: String is internally implemented as a reference type, but for usage it behaves as a value type (and thus deserves the same naming convention as you use for int, bool, etc.). See link for an example.

2018年08月22日18分20秒

Personally, I prefer lowercase string, but this answer gives a great point. string is a reference type and also immutable, so using the same convention as value types is a little weird.

2018年08月22日18分20秒