I'm sure this is an easy question for experienced programmers, but I've never had to do this before - Suppose I have a custom object looking as follows:
public class MyClass
{
public Dictionary<string,string> ToDictString()
{
Dictionary<string,string> retval = new Dictionary<string,string>;
// Whatever code
return retval;
}
public Dictionary<string,int> ToDictInt()
{
Dictionary<string,int> retval = new Dictionary<string,int>;
// Whatever code
return retval;
}
}
So, in my code, I can write something as follows:
MyClass FakeClass = new MyClass();
Dictionary<string,int> MyDict1 = FakeClass.ToDictInt();
Dictionary<string,string> MyDict2 = FakeClass.ToDictString();
And that works fine, but what I would like to be able to do is have a single method in MyClass
called, say ToDict()
that could return either type of dictionary depending on the return type expected
So, for example, I would have:
MyClass FakeClass = new MyClass();
// This would be the same as calling ToDictInt due to the return type:
Dictionary<string,int> MyDict1 = FakeClass.ToDict();
// This would be the same as calling ToDictString due to the return type:
Dictionary<string,string> MyDict2 = FakeClass.ToDict();
So, one method name, but it knows what to return based upon the variable that is to be returned... How would I write the method in my class to do I do that?
Thank you so much!!