StykPohlavsson StykPohlavsson - 5 months ago 12
Java Question

C# overriding methods in object declaration like in Java

but I've been programming in Java so I know you can Override methods in Object's instantiation

private MyObject myObject=new MyObject(){
@Override
public void myMethod(int args){
.
.
.
}
}


can somebody show me how to do this in C# please.. Thanks

Answer

so I know you can Override methods in Object's declaration

This isn't object declaration but class instantiation. And this feature doesn't exist in C# .

Therefore, you can only override polymorphic methods on class declarations:

public class A 
{
    public virtual void DoStuff() {}
}

public class B : A 
{
    public override void DoStuff() {}
}
Comments