Calling Override Overloads of Virtual Methods E-mail

Here's a C# pizzler for you -- see if you can figure it out. Do you think the assert will pass or fail? Is the wrong method getting called here?

static void Main( string[] args )
{
        Derived d = new Derived();
        d.DoSomething( 42 );
}

public class Base
{
        public virtual void DoSomething( int i )
        {}
}

public class Derived : Base
{
        public override void DoSomething( int i )
        {
                DoSomething( i, "something" );
        }
        public void DoSomething( int i, params string[] s )
        {
                Debug.Assert( s.Length > 0 );
        }
}

This code snippet creates a new object of the class Derived, which extends Base. Base has a virtual method that takes an int, which is overridden in Dreived. When we create an object of Derived and call the overridden method, we find that (because it's overridden) it's ignored completely and instead 2-argument overload is chosen. "params" arguments are optional, so this method is a viable way to handle the call, but shouldn't the sepcified override be called instead? 

 

Offers

Google Tools

Gmail Docs Code Finance Maps Calendar

More Offers