I’ve started a job as C# software engineer consultant on the 15th of this month. It’s given me the opportunity to go back over my XAML with WPF and Silverlight and doing some refactoring with C#. Doing so, I’ve been pushing my C# foo & re-discovered some things that I hadn’t use in a while and discovered new things along the way. I thought a series such as this one would force me continue to discover new things in .NET, may it be about
C#, a library/framework, Visual Studio or whatever. Today, well tonight on my end because it’s almost 11 PM, I want to talk about the following topic: aliased generics
using SimplerGenericNaming = System.Collections.Generic.HashSet<System.Collections.Generic.KeyValuePair<Namespace.SomeLongClassName, string>>;
It allows you to use
SimplerGenericNaming, instead of
HashSet<KeyValuePair<SomeLongClassName, string>> every time. It can be used when you would like to use the same big long complex generic type in a lot of places. It’ll make you type that much less and that, I really like 😀
var myShortGeneric = new SimplerGenericNaming();
Of course, it may be a little long to have to define the exact type with the namespace at the top of the file. But it does make the code simpler to read in the end in the source file. Although, there may be a way to get around this which can be cool !
using Col = System.Collections.Generic; using SimplerGenericNaming = Col.HashSet<Col.KeyValuePair<Namespace.SomeLongClassName, string>>;
This way, we get simpler naming, and aliased generics get a bit less of a hassle to use ! Do keep in mind that C# only supports closed aliases, meaning you “cannot name an unbound generic typedeclaration without supplying type arguments.” (section 9.4.1 of the C# Language spec).