How to Inherit method but with different return type?

前端 未结 10 1076
北荒
北荒 2021-01-17 19:20

Given the following classes:

ClassA
{
     public ClassA DoSomethingAndReturnNewObject()
     {}    
}

ClassB : ClassA
{}

ClassC : ClassA
{}
10条回答
  •  死守一世寂寞
    2021-01-17 19:28

    Maybe generics will be your saviour. Take a look at this link: C# Generics Part 3/4: Casting, Inheritance, and Generic Methods

    bstract class  B {
       public abstract T Fct(T t);
    }
    class D1 : B{
       public override string Fct( string t ) { return "hello"; }
    }
    class D2 : B{
       public override T Fct(T t) { return default (T); }
    }
    

提交回复
热议问题