inheritance - How to override a method Inside another method using c# -
i'm new c# , need figure out how override class method inside new class method without implementing inheritance, in java can this:
class classa{ void method1(){ statements .... .... classb obj = new classbb(){ @override void somemethod(){ //from classb statements .... .... } }; } }
i want achieve same in c#.
you cannot in c#. if part of implementation call-site specific, use delegate, though. void method2()
signature maps action
(no parameters / return-value), so:
class classb { private readonly action method2; public classb(action method2) { if(method2==null) throw new argumentnullexception("method2"); this.method2 = method2; } public void somemethod() { ... uses method2() method2(); } }
then:
classb obj = new classb(() => { // statements... });
with alternative identical syntax:
classb obj = new classb(delegate { // statements... });
or:
classb obj = new classb(delegate() { // statements... });
Comments
Post a Comment