List Aggregator App (SPFx)

Methods in C#.Net (Hiding/Virtual/Extensions Methods)

This C#.Net tutorial explains Methods in C#.net. What is the use of methods and how to write a method in C#.Net? We will also discuss Virtual methods in C#.Net and hiding methods in C#.Net. We will also discuss Extensions method in C#.Net.

Methods in C#.Net

Methods in c#.net are very much helpful to organize code, which contains one or more lines of code. Methods specify a logical task and help to manage code properly.

When you declare a method in C#.Net, the first part of the declaration specifies the data type of the return value, and the second part indicates the method name. The method also can contain parameters.

If your method doesn’t return any information, you should use the void keyword.

Examples:

void MyVoidMethod()
{
// Code goes here.
}

This method did not return any value.

int MyMethodReturnsAnInteger()
{
return 99;
}

The above method returns an interger value and in this example it returns 99.


int MyMethodReturnsAnInteger(int value)
{
return 100 + value;
}

The above method returns an integer as well as take one parameter. Here it takes a integer parameter.

Apart from this C#.Net method can specify accessibility like private, public etc.

private void MyVoidMethod()
{
// Code goes here.
}

To call methods you can directly use the method name.

MyVoidMethod(); //For void methods

int result = MyMethodReturnsAnInteger (5); //For methods that contains one parameter and returns an integer.

Hiding methods in C#.Net

We will discuss hiding methods 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 the same signature, then while accessing the method there will be a 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 hiding a method like below:

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

Virtual methods in C#.Net

Let us check now what is virtual methods in C#.Net.

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.

Extensions method in C#.Net

Now, let us discuss extensions method in C#.Net.

If your source code is available then there are different ways to extend the class like through inheritance you can add functionality to the objects. But what will happen if you do not have the source code. In C#.Net Extensions method will help us in changing the class without having the source code.

Extension methods are static methods that can appear to be part of a class without actually being in the source code for the class.

Suppose we have a class name as Employee which class has some methods defined in it. But we want another method like AddBonous (decimal bonus amount). But we do not have the source code, so we can not directly change in the assembly.

To add the method we have to create a static class as well as we have to add the method AddBonous as a static method like below:

public static class EmployeeExtension
{
public static void AddBonous (this Employee employee, decimal bonus amount)
{
employee.Salary += bonus amount;
}
}

Here the first parameter is the type that is being extended preceded by this keyword.

This is what tells the compiler that this method is part of the Employee type. Here Employee is the type that is being extended. In the extension method, you have access to all the public methods and properties of the type being extended.

But while using the method the first parameter will not come means you can access the method like obj.AddBonous (5000);


Event through the method is a static method we have to use the standard instance method syntax to call the method. But If the extension method has the same name as a method in the class, the extension method will never be called.

Hope this C#.Net tutorial helps to learn what are methods in C#.Net? How to declare and use methods in c#.net? What are virtual and hiding methods in C#.Net. Also, we saw Extensions method in C#.Net.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →