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; }
Type aliases Type aliases provide alternative names for existing types. If the type name is too long you can introduce a different shorter name and use the new one instead. It's useful to shorten long generic types.
Yes, you can define a generic method in a non-generic class in Java.
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 :-(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With