What's the difference between run-time polymorphism and compile-time polymorphism? Also, what's the difference between early binding and late binding? Examples would be highly appreciated.

- 83,810
- 28
- 209
- 234

- 10,387
- 36
- 112
- 166
-
3You want examples to google better? – H H Oct 01 '10 at 18:43
-
6Should this be tagged as `homework`? – keyboardP Oct 01 '10 at 18:46
-
Agree w/ keyboardP... Sounds like homework. – Bryce Fischer Oct 01 '10 at 18:55
-
4@keyboardP: No, while he may be asking questions you could probably locate on google these questions are asked with the intent to learn, not "do my homework for me". – Aren Oct 01 '10 at 18:57
-
@Aren: The question has been edited. In its original wording, it sounded very homework-like which is why I asked. – keyboardP Oct 01 '10 at 19:19
-
1@keyboardp: Still looks learning-oriented. He may have asked for examples but some people learn faster from code, since there's no details about what code he may need to write I don't think any example here would be "Giving" him the answer, it would be guiding him in the right direction. If you don't want to help teach then that's fine, but don't hamper someone who's trying to learn, especially when he doesn't want "just the answer". – Aren Oct 01 '10 at 19:22
-
@Aren: In what way am I hampering? I haven't voted to close this, I haven't edited the tags myself. I simply asked if this was homework. I haven't answered because I feel Justin has nailed it so no need to accuse me of trying to stop someone from learning. Not sure what you mean by "just the answer" because a lot of CS homework require code example, so that's quite a poor differentiator to bring up. I think this is a moot point now anyway. – keyboardP Oct 01 '10 at 19:27
-
[Polymorphism in C#](http://msdn.microsoft.com/en-us/library/ms173152.aspx) [Contrast with C++](http://stackoverflow.com/questions/2128838/compile-time-polymorphism-and-runtime-polymorphism) – Steve Townsend Oct 01 '10 at 18:43
4 Answers
Compile Time Polymorphism
Method overloading is a great example. You can have two methods with the same name but with different signatures. The compiler will choose the correct version to use at compile time.
Run-Time Polymorphism
Overriding a virtual method from a parent class in a child class is a good example. Another is a class implementing methods from an Interface. This allows you to use the more generic type in code while using the implementation specified by the child. Given the following class definitions:
public class Parent
{
public virtual void SayHello() { Console.WriteLine("Hello World!"); }
}
public class Child : Parent
{
public override void SayHello() { Console.WriteLine("Goodbye World!"); }
}
The following code will output "Goodbye World!":
Parent instance = new Child();
instance.SayHello();
Early Binding
Specifying the type at compile time:
SqlConnection conn = new SqlConnection();
Late Binding
The type is determined at runtime:
object conn = Activator.CreateInstance("System.Data.SqlClient.SqlConnection");

- 242,243
- 40
- 408
- 536
-
1how do .Net Generics fit into the picture here? In C++, templates express compile-time polymorphism. thx – Steve Townsend Oct 01 '10 at 19:14
A very simple and straight forward answer for the difference between compile time and run time polymorphism.
Compile Time Polymorphism - Method Overloading (Having the same method name but with different signature). Please check the video on method overloading at this URL http://csharp-video-tutorials.blogspot.com/2012/06/part-25-c-tutorial-method-overloading.html
Run Time Polymorphism - Method Overriding (Invoking the child class overridden methods at run time, using a base class reference variable is called as run time polymorphism). Please check the videos on method overriding (Polymorphism) at this URL http://csharp-video-tutorials.blogspot.com/2012/06/part-23-c-tutorial-polymorphism.html

- 291
- 2
- 2
UPDATE: Please see Eric Lippert’s comments to this answer.
In C#2 all binding is early, because C#2 is a statically-typed language. A late binding language would be one in which the method binding occurs at run time. (C#4 includes a late binding feature with the introduction of dynamic
.)
I am not sure what you mean by run time vs. compile time polymorphism.
The C# compiler will determine at compile time which method overload will be called. The run-time type of an instance will determine which implementation of a particular method overload will be executed. This is still considered early binding even though it happens at run time, because the selected method is constrained to be an implementation of a specific virtual method overload, and it is not possible for such a call to generate a type-related exception such as can occur with a dynamic language and late binding.

- 58,241
- 9
- 71
- 99
-
C# 2 supports late binding at the language level in the form of *virtual dispatch*. In virtual dispatch languages the method call is *late bound* because the *exact method* called is not determined until *runtime*. The *signature* of the method is determined at compile time but the *method actually called* is not. C# 4 supports dynamic late binding: it can determine *everything* about the method (except its name) at runtime. – Eric Lippert Oct 01 '10 at 23:32
-
Re: your update: Opinions vary. Some people consider virtual dispatch to be late binding because the method called is determined at runtime. (These tend to be people with a background in C or some other mostly static binding language.) Some people consider it to be early binding because the *slot* that will contain the method pointer is determined at compile time. (These tend to be people who are used to COM programming or C++ where almost every call is virtual.) I personally try to only use "late binding" to mean "dynamic". – Eric Lippert Oct 04 '10 at 15:07
-
@Eric Lippert - OK, that makes sense. Your use of "late binding" seems similar to mine then. I have always thought of "early" and "late" to indicate, in effect, the time at which the error is detected when something goes wrong. Thanks for your comments! – Jeffrey L Whitledge Oct 04 '10 at 15:32
-
That's a great way to make the distinction. There is a lot of confusion about what the vague term "binding" even means. I agree with your proposal: to "bind" an operation is to *perform analysis that determines type information and produces either errors or a unique best type assignment*. (Where by "type information" I include members of types, like overloaded methods.) – Eric Lippert Oct 04 '10 at 15:38
early binding and static binding is same.late binding and dynamic binding is same.dynamic binding means compiler will choose which method to call on runtime.static binding means compiler will choose method to call on compile time.when we use is-a relationship of inheritance and assign derive class reference to base class reference .we will allow to invoke or access only base class reference.roslyn service will provide keyword virtual,abstract and override to call derive class member by base class reference.

- 1
- 1