C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Difference between string and System.String?


Posted Date: 09 Aug 2004    Resource Type: Articles    Category: .NET Framework
Author: Mary MathewMember Level: Bronze    
Rating: 1 out of 5Points: 10



Difference between string and System.String

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(...)

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.



Responses

Author: Ajithkumar V Menon    12 Aug 2004Member 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


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Maximizing Application Performance under .NET Framework
Previous Resource: How to Sort Columns in the ListView based on the UserClicked Columns?
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use