Example (note the case):
string s = "Hello world!";
String s = "Hello world!";
What are the guidelines for the use of each? And what are the differences?
Example (note the case):
string s = "Hello world!";
String s = "Hello world!";
What are the guidelines for the use of each? And what are the differences?
I'd just like to add this to lfousts answer, from Ritchers book:
I didn't get his opinion before I read the complete paragraph.
Just for the sake of completeness, here's a brain dump of related information...
As others have noted,
string
is an alias forSystem.String
. They compile to the same code, so at execution time there is no difference whatsoever. This is just one of the aliases in C#. The complete list is:Apart from
string
andobject
, the aliases are all to value types.decimal
is a value type, but not a primitive type in the CLR. The only primitive type which doesn't have an alias isSystem.IntPtr
.In the spec, the value type aliases are known as "simple types". Literals can be used for constant values of every simple type; no other value types have literal forms available. (Compare this with VB, which allows
DateTime
literals, and has an alias for it too.)There is one circumstance in which you have to use the aliases: when explicitly specifying an enum's underlying type. For instance:
That's just a matter of the way the spec defines enum declarations - the part after the colon has to be the integral-type production, which is one token of
sbyte
,byte
,short
,ushort
,int
,uint
,long
,ulong
,char
... as opposed to a type production as used by variable declarations for example. It doesn't indicate any other difference.Finally, when it comes to which to use: personally I use the aliases everywhere for the implementation, but the CLR type for any APIs. It really doesn't matter too much which you use in terms of implementation - consistency among your team is nice, but no-one else is going to care. On the other hand, it's genuinely important that if you refer to a type in an API, you do so in a language neutral way. A method called
ReadInt32
is unambiguous, whereas a method calledReadInt
requires interpretation. The caller could be using a language which defines anint
alias forInt16
, for example. The .NET framework designers have followed this pattern, good examples being in theBitConverter
,BinaryReader
andConvert
classes.string
is an alias (or shorthand) ofSystem.String
. That means, by typingstring
we meantSystem.String
. You can read more in think link: 'string' is an alias/shorthand of System.String.Lower case
string
is an alias forSystem.String
. They are the same inC#
.There's a debate over whether you should use the System types (
System.Int32
,System.String
, etc.) types or theC# aliases
(int
,string
, etc). I personally believe you should use theC# aliases
, but that's just my personal preference.As the others are saying, they're the same. StyleCop rules, by default, will enforce you to use
string
as a C# code style best practice, except when referencingSystem.String
static functions, such asString.Format
,String.Join
,String.Concat
, etc...String (
System.String
) is a class in the base class library. string (lower case) is a reserved work in C# that is an alias for System.String. Int32 vs int is a similar situation as isBoolean vs. bool
. These C# language specific keywords enable you to declare primitives in a style similar to C.