Can't add keyValuePair directly to Dictionary

后端 未结 8 1140
半阙折子戏
半阙折子戏 2020-12-18 18:45

I wanted to add a KeyValuePair to a Dictionary and I couldn\'t. I have to pass the key and the value separately, which must

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-18 19:12

    There is such a method – ICollection>.Add but as it is explicitly implemented you need to cast your dictionary object to that interface to access it.

    ((ICollection>)myDict).Add(myPair);
    

    See

    • List of Explicit Interface Implementations on Dictionary's documentation page (you'll need to scroll down).
    • Explicit member implementation

    The page on this method includes an example.

提交回复
热议问题