public class A
{
public void N()
{
Console.WriteLine(typeof(T).ToString());
}
public class B : A
{
public void M()
{
Console.WriteLine(typeof(T).ToString());
}
public class C : B{ }
}
}
class MainClass
{
static void Main()
{
A
b.M();
b.N();
A
c.M();
}
}
what would be output of the above code …. ??
b.M() = System.String
b.N() = System.Int32
c.M() = System.Int32
b.M() prints System.String but c.M() prints System.Int32 !!!! isn’t it interesting ?
why does it happen ? the catch is, class AString.B.C is getting inherited from AInt.B, not AString.B, because the class AStrig.B inherited from AInt, so the compiler consider B as AInt.B not AString.B ..however you can modify the hierarchy like this code public class C : AString.B, then C will be inherited from AString.B.
For understanding purpose we can exapand the code manually as follows ...
public class AInt
{
public void N()
{
Console.WriteLine(typeof(int).ToString());
}
public class B : AInt
{
public void M()
{
Console.WriteLine(typeof(int).ToString());
}
public class C : B { }
}
}
public class AString
{
public void N()
{
Console.WriteLine(typeof(string).ToString());
}
public class B : AInt
{
public void M()
{
Console.WriteLine(typeof(string).ToString());
}
public class C : B{ }
}
}
** My special thanks to Eric Lippert(works with the C# dev team)for finding time and explaining me the issue.
No comments:
Post a Comment