Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices : What to use, .Net Class or short name? [closed]

Tags:

c#

In C# we have .Net class and the short names, like Double and double. I was suggested sometime back that we shall use the short name always, wanted to know why. I know they both are same, but why one is sometimes preferred over others? Just readability or is there something more to it?

For example if I am creating a Class having a string and a boolean properties, which of the following should be used always and when the other should be used:

Class MyClass {
    ...
    private String x;
    private Boolean y;
    ...
}

OR

Class MyClass {
    ...
    private string x;
    private bool y;
    ...
} 
like image 918
Amar Avatar asked Dec 16 '12 14:12

Amar


6 Answers

In short: Do it like you want.

I would prefer to use the alias every time I can.

Use Int32 instead of int when you have several different Ints in your Code. Like mixing Int32, Int64 or Int16.

like image 163
Johannes Wanzek Avatar answered Nov 19 '22 06:11

Johannes Wanzek


That is entirely dependent on you. For some readability is more imporatant. So use the one which is more readable and convinient to you.

Aliases are just used because of there convinience but they are the same as the actual ones for compilers.

like image 3
Rahul Tripathi Avatar answered Nov 19 '22 06:11

Rahul Tripathi


string is an alias for System.String, bool is an alias for System.Boolean and so on. The C# type keywords and their aliases are interchangeable.

I personally prefer the usage of aliases unless I need to emphasize the explicit type in case this information may be needed in the future. Such as using System.Int32 instead if int to explicitly show that there's a limitation, even if they are exactly the same.

like image 3
Oshry Avatar answered Nov 19 '22 07:11

Oshry


Well, they are the same. After compilation to CLR they both point to same datatype.

like image 3
csg Avatar answered Nov 19 '22 08:11

csg


You should you use class name when you need to access static methods and short name when declaring a variable/field of given type.

int i = 0;
Int32.TryParse(s, out i);

See any of the Microsoft code samples http://msdn.microsoft.com/en-us/library/system.int32.aspx

There is no difference in compiled code from doing it any other way. It is just cleaner and more readable.

like image 3
Ilia G Avatar answered Nov 19 '22 06:11

Ilia G


You can use either Int32 or int in your code, but you should use it in a convenient way, you shouldn't use them both, it can make the code unreadable, but C# conventions say that you should use synonyms always.

Some times, you may mix between "Wrapper Classes" those are in Java, and "Nullable Types" in .Net, or "Type Synonyms of C#"

In C# "int" and "Int32" are the same, "bool and "Boolean" are the same, "string" and "String" are the same, but "int" is the synonymous in C# language of "System.Int32" class of .Net.

You can use "int?" or Nullable<int> to make the variable accept "null" value but in Java you should use the wrapper class Integer instead of integer to hold 'null' values.

like image 3
Sawan Avatar answered Nov 19 '22 08:11

Sawan