开发者

Method signatures in classes which extend a generic class

If I have a generic class like this:

public class Repository<T>
{
  public string Greeting(T t)
  {
    return "Hi, I'm " + t.ToString();
  }
}

which is extended like this:

public class FooRepository : Repository<Foo>

If FooRepository has a method called Greeting(Foo foo), does that method have the same signature as t开发者_Python百科he base class method (i.e. hide or override it), or is it considered separate?

I'm a little confused to be honest.


It hides the base method and the compiler will warn you about this. If you want to override it you need to mark it as virtual in the base class or use the new keyword in the derived class method to indicate to the compiler that you know what you are doing and the hiding is intentional.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜