Powered by Blogger.

Saturday, March 8, 2014

Hiding methods in C#.Net




In this post we will discuss about hiding methods in C#.Net.

Also you can check out:

- Get last modified time of a file in C#.Net

- Query to get records between two dates in sql server 2008

- Steps to create wcf service and host wcf service as windows service in C#.Net

If a method with the same signature is declared in both base and derived classes but the methods are not declared as virtual and override, respectively, then the derived class version is said to hide the base class version.

Here suppose the base class method is like below:

Class Employee
{
public GetEmployee()
{
//Logic will go here
}
}

Now the derived class will be like below:

Class MyDerivedEmployee : Employee
{
public GetEmployee()
{
//Logic will go here
}
}

Now since both the method has same signeture, then while accessing the method there will be clash because your program won’t know which method to call.

So C#.Net generates a compile time warning and tell us to use the new keyword while hding a method like below:

Class MyDerivedEmployee : Employee
{
public new GetEmployee()
{
//Logic will go here
}
}



0 comments

Post a Comment