In C# what is the difference between String and string? (note the case)
Also, what are the guidelines for the use of each?
In C# what is the difference between String and string? (note the case)
Also, what are the guidelines for the use of each?
string
is an alias for System.String
. So technically, there is no difference. It's like int
vs. System.Int32
.
As far as guidelines, I think it's generally recommended to use string
any time you're referring to an object. e.g.
string place = "world";
Likewise, I think it's generally recommended to use String
if you need to refer specifically to the class. e.g.
string greet = String.Format("Hello {0}!", place);
This is the style that Microsoft tends to use in their examples.
'System.String' is THE .net string class - in C# 'string' is an alias for System.String - so in use they are the same.
As for guidelines I wouldn't get too bogged down and just use whichever you feel like - there are more important things in life and the code is going to be the same anyway.
If you find yourselves building systems where it is necessary to specify the size of the integers you are using and so tend to use Int16, Int32, UInt16, UInt32 etc. then it might look more natural to use String - and when moving around between different .net languages it might make things more understandable - otherwise I would use string and int.
Lower case string is an alias for System.String. They are the same in C#.
There's a debate over whether you should use the System types (System.Int32, System.String, etc.) types or the C# aliases (int, string, etc). I personally believe you should use the C# aliases, but that's just my personal preference.
Valters, you cannot establish global aliases in the style of string
, int
, etc. so far as I know. However, you can do more localized aliasing for types and namespaces with the using
keyword.
e.g.
using str = System.String;
//...
str s = "Now you've got another alias for string!";
See here: http://msdn.microsoft.com/en-us/library/sf0df423.aspx
@Derek, are you sure int an alias for UInt32? I would guess it is an alias for Int32.
@Lance, you are quite right. int
is an alias for Int32
. I've corrected that now.
The best answer I have ever heard about using the provided type aliases in C# comes from Jeffrey Richter in his book CLR Via C#. Here are his 3 reasons:
- I've seen a number of developers confused, not knowing whether to use string or String in their code. Because in C# the string (a keyword) maps exactly to System.String (an FCL type), there is no difference and either can be used.
- In C#, long maps to System.Int64, but in a different programming language, long could map to an Int16 or Int32. In fact, C++/CLI does in fact treat long as an Int32. Someone reading source code in one language could easily misinterpret the code's intention if he or she were used to programming in a different programming language. In fact, most languages won't even treat long as a keyword and won't compile code that uses it.
- The FCL has many methods that have type names as part of their method names. For example, the BinaryReader type offers methods such as ReadBoolean, ReadInt32, ReadSingle, and so on, and the System.Convert type offers methods such as ToBoolean, ToInt32, ToSingle, and so on. Although it's legal to write the following code, the line with float feels very unnatural to me, and it's not obvious that the line is correct:
BinaryReader br = new BinaryReader(...);
float val = br.ReadSingle(); // Ok, but feels unnatural
Single val = br.ReadSingle(); // OK and feels good
So there you have it. I think these are all really good points. I however, don't find myself using Jeffrey's advice in my own code. Maybe I am too stuck in my C# world but I end up trying to make my code look like the framework code.
It's a matter of convention, really. "string" just looks more like C/C++ style. The general convention is to use whatever shortcuts your chosen language has provided (int/Int for Int32). This goes for "object" and "decimal" as well.
Theoretically this could help to port code into some future 64-bit standard in which "int" might mean Int64, but that's not the point, and I would expect any upgrade wizard to change any "int" references to "Int32" anyway just to be safe.
There IS one difference - you can't use String without "using System;" beforehand.
I prefer the capitalized .NET types (rather than the aliases) for formatting reasons. The .NET types are colored the same as other object types (the value types are proper objects, after all).
Conditional and control keywords (like 'if', 'switch', and 'return') are lowercase and colored dark blue (by default). And I would rather not have the disagreement in use and format.
Using System types makes it easier to port between C# and VB.Net, if you are into that sort of thing.
string is a reserved word, but String is just a class name. This means that 'string' cannot be used as a variable name by itself.
For instance if for some reason you were doing this :
StringBuilder String = new StringBuilder(); // compiles
StringBuilder string = new StringBuilder(); // doesn't compile
If you really want a variable name called 'string' you can use @ as a prefix :
StringBuilder @string = new StringBuilder();
Another critical difference : Stackoverflow highlights them differently.
C# is a language which is used together with the CLR.
string is a type in C#.
System.String is a type in the CLR.
When you use C# together with the CLR string will be mapped to System.String.
Theoretically, you could implement a C#-compiler that generated Java bytecode. A sensible implementation of this compiler would probably map string to java.lang.String in order to interoperate with the Java runtime library.
Rasmus Faber has given right answer:
string is a type in C#. System.String is a type in the CLR.