2

As its known that early binding is the concept of declaring the object of specific data type, and it cant hold any other type of object. While late binding is the concept of declaring object of generic type and that can hold the instance of any other type. Consider the example:

public abstract class Animal  
{ 
 public virtual string Name { get { return "Animal"; } } 
} 

public class Dog : Animal 
{ 
  public override string Name { get { return "Dog"; } } 
} 

public class Cat : Animal 
{ 
  public override string Name { get { return "Cat"; } } 
} 

public class Test  
{ 
  static void Main() 
  {
      Animal animal = new Dog(); 
      Animal animalTwo = new Cat(); 

      Console.WriteLine(animal.Name); 
      Console.WriteLine(animalTwo.Name); 
   } 
} 

My question is THAT when will the compilor recognize the function call of object, either it will be at compile time or run time? I am sorry if i am unclear. But i mean to ask that does the concept of Virtual over riding and virtual methods involve late binding or not? .. if not then how is this possible.

Greg B
  • 14,597
  • 18
  • 87
  • 141
Talha Majid
  • 117
  • 2
  • 12

2 Answers2

5

Runtime.

A virtual function means that the dispatch is routed at runtime to the function on the live object reference. In C++ this is done via a table of pointers called a vtable. I'm not sure how C# does it, I would assume it is implementation dependant, but the result is the same.

At compile time the compiler will bind the function call to the function on the base class. That base class function is virtual, so at runtime .Net will look at the actual object type and see if it overrides that function. If it does that derived type will be called.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
  • Gaz: If i write Cat c =new Cat(); and call funtion as c.Name(); then will it be recognized at compile time?? – Talha Majid Jun 08 '12 at 09:02
  • A virtual function is virtual throughout the entire hierarchy, so c.Name would still involve a lookup of some sort in case a derived type has overriden it (eg BlackCat). – GazTheDestroyer Jun 08 '12 at 09:08
  • You mean that **Virtual dispatch is a form of late binding** i-e if a function is defined as virtual then any call to this function will be recognized at run time??? – Talha Majid Jun 08 '12 at 09:44
  • 1
    No. Late binding and dynamic dispatch are two different things. Virtual functions are bound at compile time (early), but dispatched at runtime. Late binding in C# would involve using reflection. – GazTheDestroyer Jun 08 '12 at 09:56
0

Yes, virtual methods involve late binding.

To know more about binding and what makes it late, read this post : http://blogs.msdn.com/b/ericlippert/archive/2012/02/06/what-is-quot-binding-quot-and-what-makes-it-late.aspx

Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • At Guillaume: c# involves late binding?? i read on stackoverflow only that every thing except dynamic and reflection interface is early bound in c# here is the link: [link]http://stackoverflow.com/questions/484214/c-sharp-early-and-late-binding – Sana.91 Jun 08 '12 at 09:08
  • 1
    "Virtual dispatch is a form of late binding" - Eric Lippert, a principal developer on the C# compiler team. – Guillaume Jun 11 '12 at 13:07