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;
...
}
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
.
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.
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.
Well, they are the same. After compilation to CLR they both point to same datatype.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With