0

I've been programming Java for years but now I'm working with C#. Is it possible in C# to override a function when creating an instance of an object? In Java it is possible to do this:

View v = new View(this) {
    @Override
    protected void onDraw(Canvas canvas) {
        System.out.println("large view on draw called");
        super.onDraw(canvas);
    }
};

This means that the object v will have it's method "onDraw" overrided. Is it possible to do this in C#? if yes, How?

Many Thanks!

T.Todua
  • 53,146
  • 19
  • 236
  • 237
Ricardo Alves
  • 1,071
  • 18
  • 36

1 Answers1

0

You need to do the same thing in C# - create a new version of the class - assume your original code was in a method 'testMethod' within a class 'TestClass':

internal class TestClass : SomeBaseClass
{
    internal virtual void testMethod()
    {
        View v = new ViewAnonymousInnerClassHelper();
    }

    private class ViewAnonymousInnerClassHelper : View
    {
        protected internal override void onDraw(Canvas canvas)
        {
            System.Console.WriteLine("large view on draw called");
            base.onDraw(canvas);
        }
    }
}
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28