IDictionary<,> contravariance?

前端 未结 6 729
不思量自难忘°
不思量自难忘° 2020-12-06 04:58

I have the following method in an external class

public static void DoStuffWithAnimals(IDictionary animals)

In my cal

6条回答
  •  伪装坚强ぢ
    2020-12-06 05:40

    If the dictionary's interface was read-only (allowing you only to read the key-value pairs, and couldn't even allow the ability to pull a value by its key), it could be marked with the out generic parameter modifier. If the dictionary's interface was write-only (allowing you to insert values, but not to retrieve them or iterate over them), it could be marked with the in generic parameter modifier.

    So, you can create the interface yourself and extend the Dictionary class in order to get the functionality you need.

提交回复
热议问题