KeyValuePair with generic values [closed]

First of all, List<T> accepts only one generic argument, so you cannot specify both string and KeyValuePair<string, T>. As pointed out, maybe you mean a Dictionary<K, V>?

Generally, your code with the T argument is only possible if it appears within a generic method or class that itself accepts a type argument named T.

If you expect to be able to store different elements (i.e. various types for T) in your list; that is not possible. Imagine, if every item had a different T, what advantage would typing to T make in the first place? You couldn’t make any assumptions about the values as their type would be unknown. Therefore, all you can do is specify the most specific common base type of all types that your values can possibly belong to – possibly System.Object, if they really don’t share any other common traits.

Leave a Comment