Night Walker
Night Walker

Reputation: 21260

Implement interfaces via Generics

I have following interfaces

public interface IReport<TInput, TOutput>
{
    List<TOutput> GenerateReport(TInput input); 
}


public interface IReport<TOutput>
{
    List<TOutput> GenerateReport();
}

But now I want to have following interface

public interface IReport<TInput, TOutput>
{
    TOutput GenerateReport(TInput input); 
}

Is it possible to have like this ?

Thanks

Upvotes: 1

Views: 100

Answers (3)

xanatos
xanatos

Reputation: 111850

You can't have two interfaces with the same name and same number of parameters. Note that you can't simply differentiate them with constraints.

Clearly you can change your old interface to your new specifications.

Upvotes: 0

Venemo
Venemo

Reputation: 19067

Yes, it's possible, you could even implement them with the same class. :)

However, you'll need to rename the second IReport because you already have one with that name.

If I were you, I'd move the method declaration from the new interface to the old one.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

No, that's not possible because you already have an IReport<TInput, TOutput> interface defined. And you can't move the TOutput GenerateReport(TInput input); to the first interface because it already provides a method with the same name and same input arguments. The output arguments are not taken into account when doing overloading method resolution.

But given the output arguments of your method I would simply use more meaningful names:

public interface IReport<TInput, TOutput>
{
    List<TOutput> GenerateReports(TInput input);
    TOutput GenerateReport(TInput input);
}

Upvotes: 2

Related Questions