Tuesday, September 18, 2007

 

int vs System.Int32

Many C# programmers may be a bit confused over the built in type aliases. What is the difference between int and System.Int32, or string and System.String? The answer is simple....nothing! A type alias is nothing more than a name that has been mapped directly to a system type for convienence. At compile time, the compiler simply substitutes System.Int32 for int.

Another question people often have is whether the size is or is not subject to change depending on platform. For example, if an int is a System.Int32 now, will it be a System.Int64 in the next version of .NET? The answer is no. The C# language specfication defines the mappings, so changing the size would literally require a change to the language itself. See ECMA-334 section 11.1.4 for the simple types.

Some people ask whether or not they can create their own type aliases. Well, the answer is both yes and no. The C# language does not provide a way to create global type aliases that can be accessed across all files -- there are no typedefs in the C# language! But, C# does allow you to define type aliases at the top of each source file via using statements. These type aliases, however, only exist within the context of that source file.


using myalias = MyNameSpace.MyClass;

Comments: Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]