37

How can I do the following in C#? What is the right way to write the first line of this code snippet?

using KVP<K, V> = System.Collections.Generic.KeyValuePair<K, V>;

class C { KVP<int, string> x; }
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
  • 1
    as Eric Lippert stated [here](http://stackoverflow.com/q/3720222/303290) "This is an occasionally requested feature." which hasn't made it into production yet. – mbx Jul 04 '13 at 13:35

2 Answers2

40

You can't, basically. You can only use fixed aliases, such as:

using Foo = System.Collections.Generic.KeyValuePair<int, string>;

class C { Foo x; }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

In some cases you can go with inheritance:

public class MyList<T1, T2> : List<Tuple<IEnumerable<HashSet<T1>>, IComparable<T2>>> { }

public void Meth()
{
    var x = new MyList<int, bool>();
}

Though not in your particular case, as KeyValuePair is sealed :-(

Mike Tsayper
  • 1,686
  • 1
  • 17
  • 25