EDN Admin
Well-known member
Sorry if this post is long beforehand.
So basically I have this piece of code internal class Parent
{
public virtual void Foo(int x)
{
Console.WriteLine("Parent.Foo(int x");
}
}
internal class Derived: Parent
{
public override void Foo(int x)
{
Console.WriteLine("Derived.Foo(int x)");
}
public void Foo(double y)
{
Console.WriteLine("Derived.Foo(double y)");
}
}
Now lets take a look at two pieces of code that are trying to invoke Foo method. class Program
{
static void Main(string[] args)
{
Parent p = new Derived();
p.Foo(10);
}
}
If you execute it, the output will be Dervied.Foo(int x), which is quite what we would expect.
Now lets examine second piece of code class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
d.Foo(10); // this will output Derived.Foo(double y)
}
}
This will output Derived.Foo(double y), which is honestly not what I was expecting to see. However googling around a lil bit got me to thepost of Jon Skeet on method overloading which itself got a reference to Eric Lipperts yet anothergreat post which explains why does second piece of code call Dervied.Foo(doubly y) (check up the comments at the end of the post).
In the book CLR via C# 3rd edition Jeffrey Richter explains how virtual methods are being called
Quote - "When calling a virtual instance method the JIT compiler produces some additional code in the method, which will be executed each time method is invoked.This code will first look in the variable being used to make the call and then follow the address to the calling object"
This means that if the compiler would consider Foo method call as a virtual method call in both cases, then regardless the type of variable, the output would be the same - e.g. Derived.Foo(double y), because both are referencing Derived type object in the heap. However outputs are different, so I guess that compiler interprets Foo calls differently.
So the question is - how C# compiler determines what method is called? How it decides which one of 2 Foo methods was intended to invoke?
Thanks in advance!
View the full article
So basically I have this piece of code internal class Parent
{
public virtual void Foo(int x)
{
Console.WriteLine("Parent.Foo(int x");
}
}
internal class Derived: Parent
{
public override void Foo(int x)
{
Console.WriteLine("Derived.Foo(int x)");
}
public void Foo(double y)
{
Console.WriteLine("Derived.Foo(double y)");
}
}
Now lets take a look at two pieces of code that are trying to invoke Foo method. class Program
{
static void Main(string[] args)
{
Parent p = new Derived();
p.Foo(10);
}
}
If you execute it, the output will be Dervied.Foo(int x), which is quite what we would expect.
Now lets examine second piece of code class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
d.Foo(10); // this will output Derived.Foo(double y)
}
}
This will output Derived.Foo(double y), which is honestly not what I was expecting to see. However googling around a lil bit got me to thepost of Jon Skeet on method overloading which itself got a reference to Eric Lipperts yet anothergreat post which explains why does second piece of code call Dervied.Foo(doubly y) (check up the comments at the end of the post).
In the book CLR via C# 3rd edition Jeffrey Richter explains how virtual methods are being called
Quote - "When calling a virtual instance method the JIT compiler produces some additional code in the method, which will be executed each time method is invoked.This code will first look in the variable being used to make the call and then follow the address to the calling object"
This means that if the compiler would consider Foo method call as a virtual method call in both cases, then regardless the type of variable, the output would be the same - e.g. Derived.Foo(double y), because both are referencing Derived type object in the heap. However outputs are different, so I guess that compiler interprets Foo calls differently.
So the question is - how C# compiler determines what method is called? How it decides which one of 2 Foo methods was intended to invoke?
Thanks in advance!
View the full article