Difference between string and System.StringThey 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 aliasesYou 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(...)
What is the real use of defining custom aliases ?Defining shortcuts (aliases) are helpfull if we have long namespaces and want to avoid typing the full namespace when we refer to a class name defined inside the namespace. For example, if we want to display a message box, we have to type
System.Windows.Forms.MessageBox.Show(Hello");
A common approach is, import the namespace in the top of the file as shown below:
using System.Windows.Forms;
Now we can use the messagebox as shown below:
MessageBox.Show("Hello");
The problem comes if we import another namespace which also have a messagebox class with the same name. The compiler will complain that 'MessageBox' is an ambigous reference. We can define shortcuts for each of those namespaces and refer to the class name with the shortcuts.
using f = System.Windows.Forms; using x = MyCustomNamespace;
Now we can use the messagebox as shown below:
f.MessageBox.Show("Hello"); // call the windows messagebox x.messageBox.Show("Hello"); // call the messagebox in the other namespace.
The compiler is happy because it knows exactly whcih class to use and we can save typing the full namespace everytime when we want to use this class.
|
| Author: Ajithkumar V Menon 12 Aug 2004 | Member Level: Bronze Points : 0 |
The article is good enough to throw light on the subject matter of aliases in C# and the explanation is excellent and this will reach to all of them reading it
|