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
Is namespace necessary in c#?
What are the types of methods in c#?
Can a class have static constructor?
What is the main purpose of xml?
Describe the parts of assembly.
Can abstract classes be final?
Explain about CTS?
What is using in c#?
Can a constructor have a return type?
What are the types of assembly available
What is session and cookies in c#?
What are PE(Portable Executable)?
What is entity framework c#?
Define xslt.
Explain dataset.acceptchanges method in .net?