tags:

views:

1301

answers:

8

A quick and simple question...

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?

A: 

See this question for more information.

Lasse V. Karlsen
+2  A: 

"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
A: 

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
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
says who? never heard of this convention!
BritishDeveloper
+1  A: 

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

aku
+17  A: 

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

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
+1  A: 

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

Patrik
+5  A: 

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.

IainMH
A: 

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