Powered by Blogger.

Sunday, February 23, 2014

Virtual methods in C#.Net



Here we will discuss about Virtual methods in C#.Net.

Also check out:

- How to use BalloonPopupExtender ajax control in asp.net?

- Jagged arrays in C#.Net

- Download Professional ASP.NET MVC 4 book free

If you are declaring a method to be virtual means you are allowing the methods to be overridden in any derived classes.

The syntax is same, only we need to add the virtual keyword in method signature like below:

class MyBaseClass
{
public virtual string MyVirtualMethod()
{
return "This is the base class virtual method";
}
}

Then in the derived class we have to use the override keyword to override the virtual method of the base class like below:

class MyDerivedClass: MyBaseClass
{
public override string MyVirtualMethod()
{
return "This is the override method of the parent class.";
}
}

Remember Neither member fields nor static functions can be declared as virtual.



0 comments

Post a Comment