tags:

views:

500

answers:

7

C# and VB.NET comes with built in types that maps to the CLR types. Examples are: int (C#) and Integer (VB) maps to System.Int32, long (C#) and Long (VB) maps to System.Int64. What are the best practices for deciding when to use built in types or not to use them (using the System.* structs/classes instead)?

A: 

I would always use System.Int32. You never know when Microsoft could change their minds and remap int to System.String or System.Boolean. Then you would be completed !@#%ed.

Shawn Simon
The "int, uint, byte, sbyte, bool, etc..." has exactly mapped to specific types by design.The argument "You never know when Microsoft could change their minds and remap int to System.String or System.Boolean" is missleading.
TcKs
Actually, Shawn have a point. By using the build in types, you depend on the compiler to map it to any of the .NET types. Design issues aside, you really don't know what you get until after it is compiled.
Adrian Godong
I sincerely hope Shawn is kidding.
Noldorin
Sure, you know, it's not like there's a finalized public specification for the C# language that defines what type each keyword maps onto. http://www.jaggersoft.com/csharp_standard/8.2.1.htm
mquander
This has made my day.
TheTXI
lol, I think Shawn is having a little fun. :D
jrista
...I don't get the joke...
jjnguy
Apparently the questioner doesn't get the joke either, which makes the joke pretty funny.
mquander
Surely a SO record for accepted answer with most down-votes.
JTA
I wonder if the community has effectively canceled out the positive rep from the accepted answer status yet.
TheTXI
@Adrian Godong: No, the keyword "int" has exaclty meaning. Forget the issues with "int/long" in C/C++, in managed enviroment is not the same problem.
TcKs
To those who don't get the joke - Microsoft has a Corporate limit on the number of developers it can piss off per year. Remapping "int" to System.String would exceed that limit for the next ten years. It would please the lawyers, though, so maybe it would balance out.
John Saunders
This is the most down-voted joke I've seen in a while.
Greg D
Why is this so down voted? I thought it was pretty funny.
ebrown
I think most people thought I was serious : )
Shawn Simon
@TSS: There are (a few) other accepted answers that have more downvotes: http://cloudexchange.cloudapp.net/stackoverflow/q/55
Jan Fabry
+11  A: 

The only time I would ever explicitly use "System.XYZ" in preference to a built-in type keyword is when I need an integer type of very specific size, and I want that to be clear to anyone reading my code (e.g. I might use Int32 instead of int if the integer in question is actually 4 8-bit fields packed together.)

mquander
+15  A: 

The language types (e.g. string, int, char) are simply Aliases for the CLR types (System.String, System.Int32, System.Char).

They are interchangeable, there is no need to prefer one over the other.

EDIT

The poster asked for some help in choosing between the two, very well.

Personally I tend to choose the C# language types (int, string, char etc), because they involve less typing - I suppose I'm just lazy :)

Binary Worrier
In other words. They are the exact same thing.Think: String s = "hi"; String s2 = s; s and s2 are the same string.
jjnguy
@jjnguy: This isn't about the values of S System.String s2 = "World"; s1 is the same TYPE as s2, irregardless of the fact that their values differ.
Binary Worrier
+24  A: 

I nearly always use the built-in aliases, such as int/short/long. They are easier to read, and do not require you to import System or to type System.Int32 everywhere, etc.

The language clearly defines them, and gives them a specific meaning, so I do not see any harm. However, this is 100% a personal choice.

That being said - the one place where I do explicitly use Int32, Int16, etc., is if I'm dealing with binary storage or transfer, especially to or from a custom binary format. In this case, having the explicit bitsize of each member going into and out of the file makes the code more readable and understandable, IMO.

Reed Copsey
+1: Well-stated.
Greg D
+2  A: 

Using "int" and "Int32" (and the others) are exactly same. Typicaly are used the keywords (int, Integer (vb.net), bool, etc...), because it is shorter and is highlited in IDE.

TcKs
+3  A: 

I always use the System.* types because they look more consistent between other classes - upper case first letter and the same syntax highlighting. But that's just a personal preference and just an aesthetic issue.

Daniel Brückner
A: 

Rather than when to use or not use the language types versus explicit BCL class names, it is more important to know whether or not the type you intend to use is CLS Compliant.

Specifically, the unsigned integer types are not CLS compliant because there is no requirement that a language support unsigned integer math.

Other than this wrinkle... I would recommend whichever idiom is more in keeping with your organizations code practices. If you fully namespace your type references, then I would continue that pattern with the System.* namespace... (I would also recommend against that practice, though, as it adds reader load without attendant gain in clarity).

Tetsujin no Oni