How can i pass Dictionary<string, string> to a dictionary<object,object> method?

.net, c#

Solution

You cannot pass it as is, but you can pass a copy:

var copy = dict.ToDictionary(p => (object)p.Key, p => (object)p.Value);

It is often a good idea to make your API program take an interface rather than a class, like this:

public void MyMethod(IDictionary<object, object> dObject) // <== Notice the "I"

This little change lets you pass dictionaries of other kinds, such as `SortedList<K,T>` to your API.

Problem

How can i pass a Dictionary to a method that receives a Dictionary? ``` Dictionary<string,string> dic = new Dictionary<string,string>(); //Call MyMethod(dic); public void MyMethod(Dictionary<object, object> dObject){ ......... } ```

Original source

Related problems