C# vs CLR Datatype

C# vs CLR Datatype

Source reference

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:

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)
Leave a Comment
  • Please add 5 and 2 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Comments
  • Naomi  N edited Original. Comment: Minor edit

  • Naomi  N edited Revision 1. Comment: Content is taken from stackoverflow without a reference to it

  • Naomi  N edited Revision 6. Comment: Typo fix

  • Peter Geelen - MSFT edited Revision 7. Comment: 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

  • Naomi  N edited Revision 10. Comment: Minor edit

Page 1 of 1 (5 items)
Wikis - Comment List
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Naomi  N edited Original. Comment: Minor edit

  • Naomi  N edited Revision 1. Comment: Content is taken from stackoverflow without a reference to it

  • Naomi  N edited Revision 6. Comment: Typo fix

  • Peter Geelen - MSFT edited Revision 7. Comment: 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

  • Naomi  N edited Revision 10. Comment: Minor edit

  • I see. The last quote needs to be attributed also as well as the question itself. Also, this opens a door for simply transferring many good questions from that site into TechNet Wiki. Say, today I was researching a question of how to output DataRow into a dictionary and found the answer in stackoverflow. Also, I think other articles by this user need to be checked and appropriate credit need to be added or content deleted

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

    See creativecommons.org/.../3.0 and

    See blog.stackoverflow.com/.../attribution-required

  • So, the question is - do we consider this article to be a simple plagiarism? It is compiled from answers by several people in the stackoverflow question. The question itself and the discussion is quite interesting, but what rules should we follow by re-posting such content? Is it OK to re-post it? I see many interesting questions with answers on stackoverflow

Page 1 of 1 (8 items)