7

Why does C#.Net allow the declaration of the string object to be case-insensitive?

String sHello = "Hello";
string sHello = "Hello";

Both the lower-case and upper-case S of the word String are acceptable and this seems to be the only object that allows this.

Can anyone explain why?

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
GateKiller
  • 74,180
  • 73
  • 171
  • 204
  • 3
    Firstly, it is not case-insensitive. You can’t write `STRING` or `strinG` or anything else. Secondly, it is not the only type that has an alias: `object` is an alias for `Object`; `bool` is an alias for `Boolean`; `double` is an alias for `Double`, etc. Incidentally, `void` is also an alias for `Void`, but C# doesn’t let you use `Void`... – Timwi Aug 08 '10 at 14:51
  • See [this question](http://stackoverflow.com/questions/7074/in-c-what-is-the-difference-between-string-and-string#7077) for more information. – Lasse V. Karlsen Aug 13 '08 at 12:54

7 Answers7

21

string is a language keyword while System.String is the type it aliases.

Both compile to exactly the same thing, similarly:

  • int is System.Int32
  • long is System.Int64
  • float is System.Single
  • double is System.Double
  • char is System.Char
  • byte is System.Byte
  • short is System.Int16
  • ushort is System.UInt16
  • uint is System.UInt32
  • ulong is System.UInt64

I think in most cases this is about code legibility - all the basic system value types have aliases, I think the lower case string might just be for consistency.

Keith
  • 150,284
  • 78
  • 298
  • 434
6

Further to the other answers, it's good practice to use keywords if they exist.

E.g. you should use string rather than System.String.

Iain Holder
  • 14,172
  • 10
  • 66
  • 86
2

"String" is the name of the class. "string" is keyword that maps this class.

it's the same like

  • Int32 => int
  • Decimal => decimal
  • Int64 => long

... and so on...

lubos hasko
  • 24,752
  • 10
  • 56
  • 61
1

"string" is a C# keyword. it's just an alias for "System.String" - one of the .NET BCL classes.

aku
  • 122,288
  • 32
  • 173
  • 203
1

"string" is just an C# alias for the class "String" in the System-namespace.

Patrik Svensson
  • 13,536
  • 8
  • 56
  • 77
0

I use String and not string, Int32 instead of int, so that my syntax highlighting picks up on a string as a Type and not a keyword. I want keywords to jump out at me.

Brian Leahy
  • 34,677
  • 12
  • 45
  • 60
0

string is an alias for System.String. They are the same thing.

By convention, though, objects of type (System.String) are generally refered to as the alias - e.g.

string myString = "Hello";

whereas operations on the class use the uppercase version e.g.

String.IsNullOrEmpty(myStringVariable);
ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
  • 2
    I don't think the convention is particularly to use the upper case version for operations. I certainly haven't seen that written down. The important thing is to use the BCL version for public names, e.g. ReadSingle instead of ReadFloat. – Jon Skeet Nov 21 '08 at 13:38
  • 2
    says who? never heard of this convention! – BritishDeveloper Apr 12 '10 at 21:42