I’m trying to write a Dictionary extension that works independently of the data types of Key/Value. I tried pass it by using the object data type, assuming that it will works with any type.
My code:
public static class DictionaryExtensionsClass
{
public static void AddFormat(this Dictionary< ?? unknow type/*object*/, ??unknow type/*object*/> Dic, ??unknow type/*object*/ Key, string str, params object[] arglist)
{
Dic.Add(Key, String.Format(str, arglist));
}
}
You just make the method generic:
Note that it’s only generic in the key type as the value would have to be
string(or potentiallyobjector an interface thatstringimplements, I guess) given that you’re going to add a string value to it.Unfortunately you can’t really express the constraint of “only allow value types where
stringis a valid value” in a generic type constraint.