97

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#

Community
  • 1
  • 1
BenAlabaster
  • 36,299
  • 19
  • 100
  • 147
  • 1
    Been asked several times over. Might want to search for common, possibly asked questions such as these before submitting a new one. – mmcdole Jan 16 '09 at 23:11
  • 1
    I already checked the list of previously asked questions and didn't come up with those. Thanks for pointing those out though. Very useful. – BenAlabaster Jan 16 '09 at 23:15
  • 1
    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. – BenAlabaster Jan 16 '09 at 23:20
  • 2
    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 – mmcdole Jan 16 '09 at 23:24
  • 1
    Ooh, good tip, I never thought of that... – BenAlabaster Jan 16 '09 at 23:26
  • I'm also not trying to stomp on your question or anything. Just trying to keep duplicates down and have them all point to existing posts when I can. – mmcdole Jan 16 '09 at 23:31
  • I agree that it's good to have all variations of questions linked to each other. It makes researching across similar topics far easier. – BenAlabaster Jan 16 '09 at 23:35
  • Since no one else mentioned it I hesitate to say it but I also thought they were aliases (like was already pointed out) int is an alias for Int32...now. In the future the alias could be redirected to Int64 for example and all the int's would not have to be updated but all the Int32's would need to be updated to Int64's if that is what was desired. – Phil Langeberg Aug 28 '18 at 00:42

5 Answers5

60

In C#, lower-case type names are aliases for the System.xxx type names, e.g. string equals System.String and int equals System.Int32.

It's best practice to use these language aliases for the type names instead of their framework equivalent, for the sake of consistency. So you're doing it wrong. ;-)

As for a reason why they both exist, the .NET types exist because they are defined in a language-independent standard for the .NET libraries called CTS (common type system). Why C# defines these aliases is beyond me (VB does something quite similar). I guess the two reasons are

  1. Habit. Get all these C and Java programmers to use C# by providing the same type names for some fundamental types.
  2. Laziness: You don't have to import the System namespace to use them.

EDIT Since many people seem to prefer the other notation let me point out that this is by no means unreasonable. A good case can actually be made for the usage of the CTS type names rather than C#'s keywords and some superficially good arguments are offered in the other answers. From a purity/style point of view I would probably concur.

However, consider if this is worth breaking a well-established convention that helps to unify code across projects.

Matthias Braun
  • 24,493
  • 16
  • 114
  • 144
Konrad Rudolph
  • 482,603
  • 120
  • 884
  • 1,141
16

It is conceptually similar to something like this:

using int=System.Int32
Ed S.
  • 115,705
  • 20
  • 165
  • 244
  • hmm, looks like i was too slow :) – Frederik Gheysels Jan 16 '09 at 23:10
  • 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. – styfle Oct 17 '11 at 19:19
  • 1
    @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`. – Ed S. Oct 17 '11 at 21:01
6

string is mapped to the String class AFAIK, so they're the same.

The same is true for, for example int and Int32.

Frederik Gheysels
  • 53,692
  • 9
  • 95
  • 146
4

They are both the same.

Personally I prefer using the lowercase string, the "blue one", using the C# keyword instead of the .NET class name for the same reason I'm using int instead of Int32. Also, the lowercased one doesn't require inclusion of the System namespace...

Arjan Einbu
  • 12,960
  • 1
  • 53
  • 58
4

Personally, I prefer to use String as both String and Object are references whereas all the other base types are value types. In my mind, that's the clearest separation.

Shalmanese
  • 5,096
  • 9
  • 27
  • 41
  • This is not true. string and System.String are the exact same thing: http://msdn.microsoft.com/en-us/library/ms228362.aspx – Evan M Sep 12 '12 at 14:11
  • 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. – Jim Balter Feb 27 '13 at 18:44
  • 1
    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](https://dotnetfiddle.net/FOMnW7#) for an example. – lennartk Aug 25 '15 at 08:08
  • 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. – ulu5 Mar 02 '16 at 03:27