Create alias for complex type without subclass?

I have a complex type in my program, which leads to very long lines. Here he is:

List<FruitCallback<Fruit>> 

Here's an example of where the line of code is too long and confusing:

 private static Dictionary<Type, List<FruitCallback<Fruit>>> callbacks = new Dictionary<Type, List<FruitCallback<Fruit>>>(); 

I can create an alias for it by subclassing it like this:

 class FruitCallbacks : List<SomeClass.FruitCallback<Fruit>> { } 

But I could swear that I remember reading somewhere about how to pseudo-something something like this so that an empty class is not required.

+4
source share
3 answers

You can add a fully qualified using statement to your class file (in the "Use Directives" section) for the class alias. This is a bit detailed.

 using MyType = System.Collections.Generic.List<YourNamespace.FruitCallback<YourNamespace.Fruit>>; 

And then you can MyType instead of List<FruitCallback<Fruit>> in your code.

Full working example.

 // aliased outside the namespace using MyList = System.Collections.Generic.List<Bar.FruitCallBack<Bar.Fruit>>; namespace Bar { // alternately, can be aliased inside the namespace // using MyList = System.Collections.Generic.List<FruitCallBack<Fruit>>; class Program { static void Main() { var myList = new MyList(); } } public class FruitCallBack<T> { } public class Fruit { } } 
+7
source

You can do it:

 using Foo = List<FruitCallback<Fruit>>; 

Then you can use Foo wherever you need to use List<FruitCallback<Fruit>> Example:

 using System; using System.Collections.Generic; using Foo = List<FruitCallback<Fruit>>; class Program { static void Main() { Foo f = new Foo(); } } 
+3
source

Perhaps you were thinking about type smoothing with the using statement?

 using MyType = MyCompany.MyProject.DataAccess.Interfaces.MyType; 
+1
source

All Articles