简体   繁体   中英

How can I create an alias for a generic class in C#?

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; }

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

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

class C { Foo x; }

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 :-(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM