In java, we can override or implement abstract methods upon instance creation as follows:
AbstractClass test =new AbstractClass()
{
public void AbstractMethod(string i) {
}
public void AbstractMethod2(string i) {
}
};
Is this possible in C#? if Yes, what's the equivalent code
Thanks
A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.
When the derived class inherits the abstract method from the abstract class, it must override the abstract method. This requirment is enforced at compile time and is also called dynamic polymorphism.
It is necessary to override all the methods which are declared as abstract . you cannot skip the non-concrete methods. If you really want to do then make your class as abstract. you cannot change the mechanism of abstraction.
This is because every child class must override these methods and provide an implementation of them. Thus we can declare these methods as abstract in the parent class.
This Java feature is called "anonymous class", not "an override of method on instance creation". There is no identical feature in C#.
C# took a different route - instead of providing convenience syntax for creation of subclasses, it expanded upon its delegate features, providing anonymous delegates and lambdas. Lambdas let you plug in pieces of code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With