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?

2
  • See this question for more information. Commented Aug 13, 2008 at 12:54
  • 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... Commented Aug 8, 2010 at 14:51

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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...

Comments

1

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

Comments

1

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

Comments

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);

2 Comments

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.
says who? never heard of this convention!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.