Ever wondered what is the difference between string and System.String (or, int and System.Int32) ? They are just the same and you can use either of them in your C# programs.
C# defines a set of aliases for the types in CLR, which can be treated as data types. For example, System.String is a type (class) in .NET Framework and string is an alias defined (shortcut) for this .NET class. Similary, System.Int32 is a .NET class and int is an alias defined in C# representing the System.Int32 class.
Which one to use ? CLR types or aliases ?
There is no performance reasons to choose or avoid either of them. Since C# aliases represent the same CLR class, they provide the same performace. So, it is upto you to decide which one to use. It would be a good idea to stick to a uniform pattern - either use the C# aliases or CLR types throughout your project (do not mix both) just to have an elegant looking code!
If you think you would ever manually convert your C# code to VB.NET, or if your code may be reviewed by a VB.NET guy, it would be easier if you use the CLR types (because CLR types are common for all .NET languages).
Here is some of the aliases defined in C#:
Alias CLR type
string System.String char System.Char bool System.Boolean sbyte System.SByte byte System.Byte short System.Int16 ushort System.UInt16 int System.Int32 uint System.UInt32 long System.Int64 ulong System.UInt64 decimal System.Decimal float System.Single double System.Double void System.Void
Defining custom aliases
You can define your own custom aliases in C# using the syntax shown below:
using verylong = System.Int64; using forms = System.Windows.Forms;
In the above code, we have defined two aliases. 'verylong' is an alias for the type 'System.Int64' and 'forms' is an alias for the namespace 'System.Windows.Forms'.
The alias 'verylong' can be used just like you use the class System.Int64:
verylong count = 100000;
The alias 'forms' also can be used the same way:
forms.MessageBox.Show(...)
is same as
System.Windows.Forms.MessageBox.Show(...)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|