tags:

views:

70

answers:

5

I'm writing a class that returns both a DataTable and a IEnumerable. I cannot define the interface methods exactly the same except for return type. So, I wish I could create this interface (obviously, it doesn't compile):

interface IMyInterface
    {
        DataTable GetResults();
        IEnumerable<string> GetResults();
    }

So, how would you structure these two functions, rename them, multiple interfaces, parameters?

I'm just curious on how you guys would handle it, ty...

+14  A: 

I would do this:

interface IMyInterface
{
    DataTable GetResultsAsTable();
    IEnumerable<string> GetResultsAsSequence();
}

Obviously C# doesn't allow you to have two methods whose signatures differ by return type only (interestingly the CLR does allow this). With that in mind I think it would be best to give the methods common prefixes and append a suffix that indicates the return type.

Andrew Hare
I think this is how I'm going to go with it. Thanks.
Steve
+2  A: 

Can you do this?

interface IMyInterface    { 
   DataTable GetDTResults();        
   IEnumerable<string> GetIEResults();    
}
Henry Gao
A: 

I would rename the first method:

DataTable GetResultsDataTable();
Daniel A. White
A: 

Whether you do this as an interface or as a regular class this won't compile because you have two methods with the same name with the same parameterless signature.

Besides, returning method results using two very different types have really bad design implications.

Jon Limjap
+1  A: 

You could keep the same method name by using an out parameter:

interface IMyInterface
{
     void GetResults(out DataTable results);
     void GetResults(out IEnumerable<string> results);
}
DancesWithBamboo
I marked Andrews answer but I do like you're approach too.
Steve