Cubicle.Jockey
Cubicle.Jockey

Reputation: 3328

Template'd Interfaces Conflicting

I am wondering if there is a clever trick to achieve the below code without IUseCase<in TInput> and IUseCase<out TOuput> conflicting or to simulate these cases.

public interface IUseCase<in TInput, out TOutput>
{
   TOutput Execute(TInput input);
}

public interface IUseCase<in TInput>
{
   void Execute(TInput input);
}

public interface IUseCase<out TOutput>
{
    TOutput Execute();
}

Upvotes: 1

Views: 52

Answers (1)

Alexander
Alexander

Reputation: 4173

Seems that you cannot declare two generic interfaces with same name but different template constraints, although I cannot find proof to that in MSDN and C# language specification.

Compiler would emit 'already contains a definition' error if two types differ only by covariance modifier, or by type constraint. For example, following sample does not compile as well, although generic type have different constraints:

public interface IFoo<T> where T : class
{
  T Bar();
}

public interface IFoo<T> where T : struct
{
  void Bar(T x);
}

But types considered different if number of generic parameters are different.

So the answer to your question is - no, you cannot do that, unless you rename your interfaces.

Upvotes: 1

Related Questions