Can't add keyValuePair directly to Dictionary

后端 未结 8 1084
半阙折子戏
半阙折子戏 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 18:49

    You can use the IDictionary<TKey,TValue> interface which provides the Add(KeyValuePair<TKey,TValue>) method:

    IDictionary<int, string> dictionary = new Dictionary<int, string>();
    dictionary.Add(new KeyValuePair<int,string>(0,"0"));
    dictionary.Add(new KeyValuePair<int,string>(1,"1"));
    
    0 讨论(0)
  • 2020-12-18 19:00

    Should somebody really want to do this, here is an Extension

        public static void Add<T, U>(this IDictionary<T, U> dic, KeyValuePair<T, U> KVP)
        {
            dic.Add(KVP.Key, KVP.Value);
        }
    

    but i would recommend to not do this if there is no real need to do this

    0 讨论(0)
  • 2020-12-18 19:01

    just because the enumerator for the Dictionary class returns a KeyValuePair, does not mean that is how it is implemented internally.

    use IDictionary if you really need to pass KVP's because you've already got them in that format. otherwise use assignment or just use the Add method.

    0 讨论(0)
  • 2020-12-18 19:06

    I'm not 100% sure, but I think the internal implementation of a Dictionary is a Hash-table, which means key's are converted to hashes to perform quick look ups.

    Have a read here if you want to know more about hashtables

    http://en.wikipedia.org/wiki/Hash_table

    0 讨论(0)
  • 2020-12-18 19:08

    Unless I'm mistaken, .NET 4.5 and 4.6 adds the ability to add a KeyValuePair to a Dictionary. (If I'm wrong, just notify me and I'll delete this answer.)

    https://msdn.microsoft.com/en-us/library/cc673027%28v=vs.110%29.aspx

    From the above link, the relevant piece of information is this code example:

    public static void Main() 
    {
        // Create a new dictionary of strings, with string keys, and 
        // access it through the generic ICollection interface. The 
        // generic ICollection interface views the dictionary as a 
        // collection of KeyValuePair objects with the same type 
        // arguments as the dictionary. 
        //
        ICollection<KeyValuePair<String, String>> openWith =
            new Dictionary<String, String>();
    
        // Add some elements to the dictionary. When elements are  
        // added through the ICollection<T> interface, the keys 
        // and values must be wrapped in KeyValuePair objects. 
        //
        openWith.Add(new KeyValuePair<String,String>("txt", "notepad.exe"));
        openWith.Add(new KeyValuePair<String,String>("bmp", "paint.exe"));
        openWith.Add(new KeyValuePair<String,String>("dib", "paint.exe"));
        openWith.Add(new KeyValuePair<String,String>("rtf", "wordpad.exe"));
    
        ...
    }
    

    As can be seen, a new object of type Dictionary is created and called openWith. Then a new KVP object is created and added to openWith using the .Add method.

    0 讨论(0)
  • 2020-12-18 19:12

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

    ((ICollection<KeyValuePair<KeyType, ValueType>>)myDict).Add(myPair);
    

    See

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

    The page on this method includes an example.

    0 讨论(0)
提交回复
热议问题