Lets consider I have Class B and C which is inherited from class B:
namespace ConsoleApp1
{
class B
{
public void foo(B obj)
{
Console.WriteLine("B1 ");
}
public void foo(C obj)
{
Console.WriteLine("B1 ");
}
}
class C : B
{
public void foo(B obj)
{
Console.WriteLine("C1 ");
}
public void foo(C obj)
{
Console.WriteLine("C1 ");
}
}
public class Program
{
static void Main(string[] args)
{
B c = new C();
B b = new B();
b.foo(c);
c.foo(b);
c.foo(c);
Console.ReadLine();
}
}
}
I am getting these results:
B1
B1
B1
I don't understand what happened exactly specially for the part:
c.foo(b); // prints B1
c.foo(c); // prints B1
Note: this question is just a mirror of the java version which prints:
B1
C1
C1
It will be great if someone can explain why C# print different values than Java for exactly same OOP code!