标签云

微信群

扫码加入我们

WeChat QR Code


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

2018年10月24日52分13秒

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年10月23日52分13秒

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年10月23日52分13秒

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年10月23日52分13秒

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

2018年10月23日52分13秒

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

2018年10月23日52分13秒

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

2018年10月23日52分13秒

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

2018年10月24日52分13秒

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年10月24日52分13秒

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

2018年10月23日52分13秒

hmm, looks like i was too slow :)

2018年10月23日52分13秒

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年10月24日52分13秒

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年10月24日52分13秒

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

2018年10月24日52分13秒

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年10月24日52分13秒

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年10月24日52分13秒

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年10月24日52分13秒