Type aliasing in using declarations
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:
With.cs
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
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