Key value pairs in C# Params

前端 未结 8 2421
后悔当初
后悔当初 2021-02-11 14:55

I\'m looking for a way to have a function such as:

myFunction({\"Key\", value}, {\"Key2\", value});

I\'m sure there\'s something with anonymous

8条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-11 15:31

    Here's more of the same:

    static void Main(string[] args)
    {
        // http://msdn.microsoft.com/en-us/library/bb531208.aspx
        MyMethod(new Dictionary()
        {
            {"key1","value1"},
            {"key2","value2"}
        });
    }
    
    static void MyMethod(Dictionary dictionary)
    {
        foreach (string key in dictionary.Keys)
        {
            Console.WriteLine("{0} - {1}", key, dictionary[key]);
        }
    }
    

    Some details on initialising a dictionary can be found here.

提交回复
热议问题