C# vs CLR Datatype

Credits

Content below is a transcript of the following discussion http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string 

All the content contributed to Stack Overflow, Stack Overflow Meta, Server Fault, and Super User is cc-wiki (aka cc-by-sa) licensed, intended to be shared and remixed.

(under cc-wiki with attribution required)

What is the difference between int, Int32 or Int64 and long or string vs. String ?

The answer is pretty simple. All are the same. First one is C# datatype and other is CLR dataType.

When we compiled C# code, C# datatype converts to CLR data type. So you have freedom to use any one of them. There is no performance difference.

(by Jon Skeet) As others have noted, string is an alias for System.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:

Type

system

object System.Object
string System.String
bool System.Boolean
byte System.Byte
sbyte System.SByte
short System.Int16
ushort System.UInt16
int System.Int32
uint System.UInt32
long System.Int64
ulong System.UInt64
float System.Single
double System.Double
decimal System.Decimal
char System.Char

Apart from stringobject, 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 is System.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:

public enum Foo : UInt32 {} // Invalid public enum Bar : uint   {} // Valid

(Luke Foust says) 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 ReadBooleanReadInt32ReadSingle, and so on, and the System.Convert type offers methods such as ToBooleanToInt32ToSingle, 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

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. (this is from the last answer on the first page of that question).