Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#
Tip/Trick

Type aliasing in using declarations

Rate me:
Please Sign up or sign in to vote.
4.71/5 (9 votes)
3 Oct 2011CPOL 29.4K   7
Type aliasing in using declarations
Recently, I was working on a project that used a mixture of Winforms and WPF controls. I found myself constantly having to qualify types that had identical names to types in other namespaces. For example, the Color class is present in both the System.Windows.Media namespace (WPF) and the System.Drawing namespace, if both these namespaces are added in the 'using' section, then an attempt to reference the Color class without qualifying it with a namespace will result in the following error:

'Color' is an ambiguous reference between 'System.Drawing.Color' and 'System.Windows.Media.Color'


This was getting annoying so I did some research and found a small bit of functionality that I hadn't seen used before:

C#
using [type alias] = [namespace].[type]


For example:

C#
using WPFColor = System.Windows.Media.Color;


The result of this is that the compiler recognizes the alias as the declared type, allowing the following:

C#
System.Drawing.Color winformsColour = System.Drawing.Color.Aqua;
System.Windows.Media.Color wpfColour = new System.Windows.Media.Color();


to be simplified to:
C#
Color winformsColour = Color.Aqua;
WPFColor wpfColour = new WPFColor();


It should be noted that this alias is a pre-compilation technique that has no effect on the compiled code. To test this, I created 2 classes as follows:

With.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using WPFColour = System.Windows.Media.Color;

namespace WindowsFormsApplication1
{
    class With
    {
        public void Foo()
        {
            Color winformsColor = Color.AntiqueWhite;
            WPFColour wpfColor = new WPFColour();

            Console.WriteLine(winformsColor.ToString() + wpfColor.ToString());
        }
    }
}


Without.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class Without
    {
        public void Foo()
        {
            System.Drawing.Color winformsColor = System.Drawing.Color.AntiqueWhite;
            System.Windows.Media.Color wpfColor = new System.Windows.Media.Color();

            Console.WriteLine(winformsColor.ToString() + wpfColor.ToString());
        }
    }
}


Both of these classes compiled to the same, identical, IL:

C#
.method public hidebysig 
	instance void Foo () cil managed 
{
	// Method begins at RVA 0x2180
	// Code size 51 (0x33)
	.maxstack 2
	.locals init (
		[0] valuetype [System.Drawing]System.Drawing.Color winformsColor,
		[1] valuetype [PresentationCore]System.Windows.Media.Color wpfColor
	)

	IL_0000: call valuetype [System.Drawing]System.Drawing.Color [System.Drawing]System.Drawing.Color::get_AntiqueWhite()
	IL_0005: stloc.0
	IL_0006: ldloca.s wpfColor
	IL_0008: initobj [PresentationCore]System.Windows.Media.Color
	IL_000e: ldloca.s winformsColor
	IL_0010: constrained. [System.Drawing]System.Drawing.Color
	IL_0016: callvirt instance string [mscorlib]System.Object::ToString()
	IL_001b: ldloca.s wpfColor
	IL_001d: constrained. [PresentationCore]System.Windows.Media.Color
	IL_0023: callvirt instance string [mscorlib]System.Object::ToString()
	IL_0028: call string [mscorlib]System.String::Concat(string, string)
	IL_002d: call void [mscorlib]System.Console::WriteLine(string)
	IL_0032: ret
} // end of method Without::Foo

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Parkings Solutions Limited
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Azim Zahir25-Jun-13 22:17
Azim Zahir25-Jun-13 22:17 
GeneralReason for my vote of 1 And tomorrow somebody will explain u... Pin
Yet Another XCoder15-Sep-11 6:12
Yet Another XCoder15-Sep-11 6:12 
GeneralRe: thank you for adding a comment to your 1 vote. I would say t... Pin
GParkings15-Sep-11 9:45
GParkings15-Sep-11 9:45 
thank you for adding a comment to your 1 vote. I would say though that, although possibly obvious to you, I had not seen this used in 7 years of software development. That is why I figured it warranted a tip.

GeneralReason for my vote of 5 I knew I could alias a namespace, bu... Pin
KenBonny13-Sep-11 23:25
KenBonny13-Sep-11 23:25 
GeneralHave 4 - good tip, but see my alternative for why I don't th... Pin
Reiss5-Sep-11 21:25
professionalReiss5-Sep-11 21:25 
GeneralRe: Given that the author describes his tip as Type aliasing, I ... Pin
Pete O'Hanlon5-Sep-11 23:16
mvePete O'Hanlon5-Sep-11 23:16 
Questionnice tip ! Pin
__erfan__12-Sep-11 18:48
__erfan__12-Sep-11 18:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.