How can we Achieve Late binding in C#.Can any give one example.
Answer Posted / saurabh
Its using Virtual functions.
When compiler encounters virtual keyword in an function
defination, instead of binding to the function directly,
the compiler writes a bit of dispatch code that at runtime
will look at calling objects realtype and calls the
function accordingly.
EX.
class baseClass
{
protected virtual void PrintMessage()
{
Console.WriteLine("Hi From Base Class");
}
}
class derivedClass : baseClass
{
protected override void PrintMessage()
{
Console.WriteLine("Hi From Derived Class");
}
}
public static void Main()
{
baseClass b = new baseClass();
baseClass bd = new derivedClass();
b.PrintMessage(); // prints "Hi From Base Class"
bd.PrintMessage(); // prints "Hi From Derived Class"
}
Here the runtime detects the correct type of object stored
in bd i.e. derivedClass and calls dericedClass
implementation of PrintMessage().
| Is This Answer Correct ? | 20 Yes | 4 No |
Post New Answer View All Answers
From which base class do all Web Forms inherit from?
What are callback methods in c#?
Whats an assembly? Describe the importance of assembly?
What is the difference between User controls and Custom Controls?
What is activator c#?
What is the difference between a method and a property?
Is it possible to have different access modifiers on the get/set methods of a property in c#?
Explain the process of abstraction with an example?
What is difference between yielding and sleeping?
You are designing a user control. You created new property called backgroundimage which is of type image. You wanted to disable storing this property in the user’s form. How to achieve this?
how to compare numbers and dispaly the largest ? *first thing I wanted to do is to input how many numbers to be compared *and then analyzed the largest then display it.
Why dictionary is used in c#?
What is float in unity?
What is gac? What are the steps to create an assembly and add it to the gac?
Why array is faster than arraylist in c#?