Type Aliases in C#
Under the category of "you learn something new every day": While watching dnrTV this morning, I saw something that I've never seen done in C# before.
I knew that it was possible to make an "alias" for a namespace in C#. For example, if you have a complex namespace like MadProps.Windows.Controls (a contrived example) that you don't want to add to your "using" list, but you don't want to have to retype every time, you can use code like this to "alias" it:
using MPControls = MadProps.Windows.Controls;
That means that instead of typing "MadProps.Windows.Controls.TextBox" all through your code, you can just type "MPControls.TextBox". Nice.
What I learned this morning, however, is that you can do the same thing with types! If you have a type name that's really complex (think generic types) you can create an alias for them, too!
using MyDict = System.Collections.Generic.Dictionary<int, MyLongClassName>;
Now I can just declare a variable as type "MyDict" in code instead of retyping the full dictionary definition!
Granted the uses are limited. Unless it's a particularly nasty type name I think your code readability would be best served by avoiding these sorts of aliases. However, it's great to know that the feature exists.
# Trackback from Jason Haley on 15/01/2008 1:24 AM
# Trackback from Reflective Perspective - Chris Alcock » The Morning Brew #10 on 15/01/2008 6:00 PM
# Trackback from Weekly Link Post 25 « Rhonda Tipton’s WebLog on 21/01/2008 10:43 AM
Comments
# David Parslow
15/01/2008 3:35 AM
I also didn't know about the type aliasing. I would agree that most times aliases should be avoided. Most times that I have seen namespace aliases is for class name collisions (and also for no intelligent reason). At that point, I often had wished that the person designing that library had named their classes better. I do wonder if type aliasing can be helpful for transitioning a badly named typed to a better name.
Note: TextBox is not a great name for a third party library class because it collides with Microsoft classes.
# Isaac Rodriguez
19/01/2008 4:52 PM
First of all, thanks for the tip. Like you, I did know about namespace aliases, but I did not know you could do it with types too.
I do disagree with your opinion that its use is limited and that makes code less readable. As a matter of fact, I believe is the other way around; it improves code readability, especially with Generics.
This feature is similar to 'typedef' in C++, which is very useful, not only to reduce the amount of typing, but to give better names to template instances. System.Collections.Generic.Dictionary<int, string> can hold many different things, but using ClientIdMap = System.Collections.Generic.Dictionary<int, string> clearly expresses what you are using the dictionary for.
Great tip. Thanks again,
# Rahul
11/12/2009 11:24 PM
I found good resources of c#. Check this out
http://csharptalk.com
# Pablo Straub
16/07/2013 1:01 PM
Using with a type is not just shorthand to avoid typing. It is also to really express the meaning of what you do. While doing a program to solve Sudoku I needed a class that expresses the possible values of a cell, that is, a set of values where each value is a number from 1 to 9.
This can be achieved nicely with
alias ValueSet = System.Collections.Generic.HashSet<byte>;
which is a better name because it shows why we are using HashSet. This is similar to what Isaac Rodriguez mentions above.
# John Huff
5/01/2014 3:10 AM
I agree w/ Paulo Straub.
I use it to add convention info to the type name, such as
using DecimalInPercent = System.Decimal;
Now that a decimal variable should be passed in as 5.0 instead of 0.05 for five percent has been documented concisely.