2020年12月24日星期四

What's different between generic and polymorphism in C# method's arguments?

I have a class need to use a object, and the object is constraint of some kind of class. However, I have two design for this method to implement: generic and polymorphism, then the example code like below:

class S3 : S{}  class S2 : S{}  class S{}    class C : A{      public override void DoOne(S s){          var tmp = s as S3;      }      public override void DoTwo<T>(T s){          var tmp = s as S3;      }  }  class B : A{      public override void DoOne(S s){          var tmp = s as S2;      }      public override void DoTwo<T>(T s){          var tmp = s as S2;      }  }  abstract class A{      public abstract void DoOne(S s);      public abstract void DoTwo<T>(T s) where T : S;  }    var s2 = new S2();  var b = new B();  b.DoOne(s2);  b.DoTwo(s2);  

The class B need use S2 object, and maybe I have class C need use S3 object. I don't know what's better for implement those.

https://stackoverflow.com/questions/65444637/whats-different-between-generic-and-polymorphism-in-c-sharp-methods-arguments December 25, 2020 at 10:01AM

没有评论:

发表评论