Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

Can I use ReaderWriterLock instead of Monitor.Enter/Exit for Threading?

1024


Explain the 3 types of properties in c# with an example?

1136


What's the difference between class and object?

1045


Explain the role of the datareader class in ado.net connections?

945


What is delimiter in c#?

946


What is lambda expression in c#?

1013


Is string a primitive data type in c#?

909


Can abstract class have private constructor c#?

1019


What is delegates and events?

994


What is difference between write and writeline?

953


What is oledb in c#?

991


What is datagrid c#?

1006


How many constructor can a class have?

1052


How can I process command-line arguments?

1023


what are the Disadvantages of vb

1102