Click here to Skip to main content
13,055,754 members (48,379 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 5 Sep 2011

Type aliasing in using declarations

, 3 Oct 2011
Rate this:
Please Sign up or sign in to vote.
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:

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

For example:

using WPFColor = System.Windows.Media.Color;

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

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

to be simplified to:
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:

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

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:

.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


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


About the Author

Founder Parkings Solutions Limited
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Azim Zahir25-Jun-13 22:17
memberAzim Zahir25-Jun-13 22:17 
GeneralReason for my vote of 1 And tomorrow somebody will explain u... Pin
rmiguel15-Sep-11 6:12
memberrmiguel15-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
memberGParkings15-Sep-11 9:45 
GeneralReason for my vote of 5 I knew I could alias a namespace, bu... Pin
KenBonny13-Sep-11 23:25
memberKenBonny13-Sep-11 23:25 
GeneralHave 4 - good tip, but see my alternative for why I don't th... Pin
Reiss5-Sep-11 21:25
memberReiss5-Sep-11 21:25 
GeneralRe: Given that the author describes his tip as Type aliasing, I ... Pin
Pete O'Hanlon5-Sep-11 23:16
protectorPete O'Hanlon5-Sep-11 23:16 
Questionnice tip ! Pin
__erfan__12-Sep-11 18:48
member__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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by GParkings
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid