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

1
  • I chose Shawn's answer because it provide me with solid ground of choosing between the two options. Other answer merely state "it's equal, use either" -- this is not really helpful, IMO. If I can pick the second best answer, it would be Reed's. Anyway, since people are complaining, I'll let more answers coming in... May 26, 2009 at 16:44

6 Answers 6

37

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.

0
15

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

2
  • In other words. They are the exact same thing. Think: String s = "hi"; String s2 = s; s and s2 are the same string.
    – jjnguy
    May 26, 2009 at 16:33
  • @jjnguy: This isn't about the values of S & S2 being equal, ignore the values. I'm saying the types that hold the values (not the values) are identical. i.e. string s1 = "Hello"; System.String s2 = "World"; s1 is the same TYPE as s2, irregardless of the fact that their values differ. May 26, 2009 at 16:48
14

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

6

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.

2

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.

1

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

1
  • And 6 years later: the StyleCop ruleset says use the builtins. Apr 7, 2015 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.