Nexus's Threadpool

There is not one, there are many ...

Method hiding in c# or the use of new keyword.

Definition: Method hiding or simply put if a method is not overriding the derived method, it is hiding it.
Snippet 1:

using System;

namespace Polymorphism
{
    class Super { public void SomeMethod()
        {
            Console.WriteLine("Super Write");
        }
    }

    class Sub : Super
    {
        public new void SomeMethod()
        {
            Console.WriteLine("Sub Write");
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            Super lSuper = new Super();
            Sub lSub = new Sub();

            lSuper.SomeMethod();
            lSub.SomeMethod();
        }
    }
}

The using of the new keyword is optional as it will only generate a compiler warning. Over to example 2


Snippet 2:
namespace Polymorphism
{
    public abstract class Super
    {
        public void SomeMethod()
        {
            Console.WriteLine("Super Write");
        }
    }

    public class Sub : Super
    {
        public new void SomeMethod()
        {
            Console.WriteLine("Sub Write");
        }
    }

    public class Program
    {
        private static void Main(string[] args)
        {
            Sub lSub = new Sub();

            lSub.SomeMethod(); //Which will output "Sub Write"
        }
    }
}


And again the use of the new keyword is optional and will only generate a compiler warning.  Now why does this concerns me as a developer.
Suppose you have developed a framework with an abstract super class like in snippet 3


Snippet 3:
using System;
using Polymorphism;

namespace Polymorphism
{
    public abstract class Super
    {

        protected void SomeRatherImportantSuperStuff()
        {
            Console.WriteLine("Super Write");
            //Very important code !!! -- here
            DoSubStuff();
        }

        public abstract void DoSubStuff();
    }

    public class Sub : Super
    {
        public new void SomeRatherImportantSuperStuff()
        {
            Console.WriteLine("Sub Write");
        }

        public override void DoSubStuff()
        {
            Console.WriteLine("Sub Write");
        }
    }

    public class Program
    {
        private static void Main(string[] args)
        {
            Sub lSub = new Sub();

lSub.SomeRatherImportantSuperStuff(); //Which will output "Sub Write" and NOT first Super Write like we wanted
        }
    }
}


So a quick word of advice here make visual studio threat the warning of the not using a new keyword as a build error, like you are used to for the public method missing comment warning.
Just yet another new feature that can cause serious issues and break more or less your inheritance. Apparantly you can even hide a sealed method !!!
 

 

Posted: Feb 14 2008, 01:16 AM by Nexus | with 2 comment(s)
Filed under:
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: