Information Technology Reference
In-Depth Information
Clearly, the following code prints “In B.Bar”:
var obj1 = new D ();
obj1.Bar( new D2 ());
Now, add a different overload, and include an optional parameter:
public class D : B
{
public void Foo( B2 parm)
{
Console .WriteLine( "In D.Foo" );
}
public void Bar( B2 parm1, B2 parm2 = null )
{
Console .WriteLine( "In D.Bar" );
}
}
Hopefully, you've already seen what will happen here. This same snippet
of code now prints “In D.Bar” (you're calling your derived class again):
var obj1 = new D ();
obj1.Bar( new D2 ());
The only way to get at the method in the base class (again) is to provide a
cast in the calling code.
These examples show the kinds of problems you can get into with one
parameter method. The issues become more and more confusing as you
add parameters based on generics. Suppose you add this method:
public class B
{
public void Foo( D2 parm)
{
Console .WriteLine( "In B.Foo" );
}
public void Bar( B2 parm)
{
Console .WriteLine( "In B.Bar" );
}
 
Search WWH ::




Custom Search