Wednesday, March 28, 2007

Getting correct object type

Following code snippet demonstrates how can we use GetType function to get correct object type in inheritance hierarchy

public interface A
{
void print();

}

public class B : A
{
void A.print()
{
Console.WriteLine("B");
}
}

public class C : A
{
void A.print()
{
Console.WriteLine("C");
}
}


public class MyClass
{
public static void Main()
{
A a = new C();
Console.WriteLine("#: " + a.GetType().Name);
a.print();
RL();
}

}

No comments: