Inheritence in C# question - is overriding internal methods possible?

Posted by Jeff Dahmer on Stack Overflow See other posts from Stack Overflow or by Jeff Dahmer
Published on 2010-04-02T00:18:20Z Indexed on 2010/04/02 0:23 UTC
Read the original article Hit count: 590

Filed under:
|

Is it possible to override an internal method's behavior?

using System;

class TestClass
{
    public string Name { get { return this.ProtectedMethod(); } }

    protected string ProtectedMethod()
    {
        return InternalMethod();
    }

    string InternalMethod()
    {
        return "TestClass::InternalMethod()";
    }
}

class OverrideClassProgram : TestClass
{   // try to override the internal method using new? (compiler warning)        
    new string InternalMethod()
    {
        return "OverrideClassProgram::InternalMethod()";
    }

    static int Main(string[] args)
    {
        // TestClass::InternalMethod()
        Console.WriteLine(new TestClass().Name);
        // TestClass::InternalMethod() ?? are we just screwed?
        Console.WriteLine(new OverrideClassProgram().Name); 
        return (int)Console.ReadKey().Key;
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about inheritance