A dictionary where value is an anonymous type in C#

前端 未结 5 534
鱼传尺愫
鱼传尺愫 2020-12-01 04:26

Is it possible in C# to create a System.Collections.Generic.Dictionary where TKey is unconditioned class and TValue

5条回答
  •  庸人自扰
    2020-12-01 04:52

    As itowlson said, you can't declare such a beast, but you can indeed create one:

    static IDictionary NewDictionary(TKey key, TValue value)
    {
        return new Dictionary();
    }
    
    static void Main(string[] args)
    {
        var dict = NewDictionary(new {ID = 1}, new { Column = "Dollar", Localized = "Доллар" });
    }
    

    It's not clear why you'd actually want to use code like this.

提交回复
热议问题