I have a simple base class and derived class:
class Base
{
public virtual void Write(int value)
{
Console.WriteLine("base int: {0}", value);
}
public virtual void Write(string value)
{
Console.WriteLine("base string: {0}", value);
}
}
class Derived : Base
{
public override void Write(int value)
{
Console.WriteLine("derived int: {0}", value);
}
public virtual void Write(IEnumerable enumerable)
{
Console.WriteLine("derived IEnumerable: {0}", enumerable);
}
public virtual void Write(object o)
{
Console.WriteLine("derived obj: {0}", o);
}
}
If I run this:
static void Main(string[] args)
{
Derived d = new Derived();
Console.WriteLine("derived:");
d.Write(42);
d.Write("hello");
Console.WriteLine("base:");
Base b = d;
b.Write(42);
b.Write("hello");
}
I get:
derived:
derived obj: 42
derived IEnumerable: hello
base:
derived int: 42
base string: hello
But I am expecting "b.Write(42)" and "d.Write(42)" to be identical. The same for the string case.
What am I not understanding? How can I get the behavior to be what I am expecting given the constraint that I cannot modify "Base"?
UPDATE: See Eric's post.