Question

I'm looking through the iDesign c# Coding Standards, and came across a rule that is not explained, but curious:

Always use C# predefined types rather than the aliases in the System namespace. For Example:

object NOT Object
string NOT String
int    NOT Int32

In my personal experience, I tend to use the predefined types, having come from more of a C background, but I am curious as to why this is a rule. The only reasoning I can think of is that, as the name "standard" suggests, it is simply a way of saying use this style, and not that one, which helps with programmers inter-mixing the two together (string myString = String.Empty).

Are there other reasons for this that I'm not seeing?

Thanks!

Was it helpful?

Solution

From an embedded background, using Int32 (instead of int) means that someone wrote the code specifically with the intent that the integer value be represented with 32 bits so care must be taken in future code that may expand or contract the size.

So, in C#, if I were using an integer where I didn't have a particular reason to specify a bit size, I'd use int. Future programmers could decide they want an int64 without breaking things (decreasing bit size should always be checked).

As for object/Object and String/string, some guidelines I've seen advocate using string or object for a particular object and String/Object when you are referring to the class.

string cat = "meow";
boolean gotCat = String.IsNullOrEmpty(cat);

OTHER TIPS

One of the key reasons for coding standards is for consistency. By enforcing this standard you make sure that code is consistent.

I don't think there's any reason beyond that. In terms of performance there's no difference between string and String.

I also agree with Kirk and that people may assume that int and Int32 are different and try and cast between them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top