Type parameter 'T' has the same name as the type parameter from outer type '…'

前端 未结 3 1179
执念已碎
执念已碎 2021-01-07 19:06
public abstract class EntityBase { ... }

public interface IFoobar
{
    void Foo(int x)
        where T : EntityBase, new();
}

public interface IFoobar<         


        
3条回答
  •  囚心锁ツ
    2021-01-07 19:26

    Just try

    void IFoobar.Foo(int x) { Foo(x); }
    

    Of course, that still doesn't guarantee that U is the same as T. You can't enforce that at compile-time, because when you're implementing an interface, you must follow its rules -- and IFoobar doesn't put such a restriction on Foo, and if you do, you would no longer be implementing the interface (by definition, since you are being stricter, and yet you're claiming that you're not).

    You can try checking it at run time instead, although that's somewhat "cheating" (since you're not really conforming to the interface then either).

提交回复
热议问题