Type Of Methods In C#

In programming language, there are terms called method and function. But in C# we use the term "Method."

Method is a body where we put the logic to get some work done.

Here is the list of Method type in C#.

  1. Pure virtual method.
  2. Virtual method.
  3. Abstract method.
  4. Partial method.
  5. Extension method.
  6. Instance method.
  7. Static method.

Pure Virtual Method

Pure virtual method is the term that programmers use in C++. There is a term "abstract" in place of "pure virtual method" in C#.

Example: 

  1. public void abstract DoSomething();  
Virtual Method

Virtual method makes some default functionality. In other words, virtual methods are being implemented in the base class and can be overridden in the derived class.

Example:
  1. public class A  
  2. {  
  3.     public virtual int Calculate(int a, int b)  
  4.     {  
  5.         return a + b;  
  6.     }  
  7. }  
  8. public class B: A  
  9. {  
  10.     public override int Calculate(int a, int b)  
  11.     {  
  12.         return a + b + 1;  
  13.     }  
  14. }  
Abstract Method

Abstract Method is the method with no implementation and is implicitly virtual. You can make abstract method only in Abstract class.

Example:
  1. public void abstract DoSomething(int a);  
Partial Method

A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type.

Example: 
  1. namespace PM  
  2. {  
  3.     partial class A  
  4.     {  
  5.         partial void OnSomethingHappened(string s);  
  6.     }  
  7.     // This part can be in a separate file.   
  8.     partial class A  
  9.     {  
  10.         // Comment out this method and the program   
  11.         // will still compile.   
  12.         partial void OnSomethingHappened(String s)  
  13.         {  
  14.             Console.WriteLine("Something happened: {0}", s);  
  15.         }  
  16.     }  
  17. }  
Extension Method

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

Extension methods are used to add some functionality to given type.
 
Note
  1. For making Extension methods, you need to have a static class with static method.
  2. Do not make Extension method for one or two lines of code, write Extension method for logic.

E.g.

  1. public static class ExtensionMethods  
  2. {  
  3.     public static string UppercaseFirstLetter(this string value)  
  4.     {  
  5.         //  
  6.         // Uppercase the first letter in the string.  
  7.         //  
  8.         if (value.Length > 0)  
  9.         {  
  10.             char[] array = value.ToCharArray();  
  11.             array[0] = char.ToUpper(array[0]);  
  12.             return new string(array);  
  13.         }  
  14.         return value;  
  15.     }  
  16. }  
  17. class Program  
  18. {  
  19.     static void Main()  
  20.     {  
  21.         //  
  22.         // Use the string extension method on this value.  
  23.         //  
  24.         string value = "deeksha sharma";  
  25.         value = value.UppercaseFirstLetter();  
  26.         Console.WriteLine(value);  
  27.     }  
  28. }  
Instance method

An instance method operates on a given instance of a class, and that instance can be accessed as this.
  1. public class PropertyUtil  
  2. {  
  3.     public void DoSomething()  
  4.     {  
  5.         // Do Something.  
  6.     }  
  7.     public void DoSomethingElse()  
  8.     {  
  9.         this.DoSomething(); // Calling to instance method.  
  10.     }  
  11. }  
  12. public class Program  
  13. {  
  14.     static void Main()  
  15.     {  
  16.         PropertyUtil util = new PropertyUtil();  
  17.         util.DoSomething(); // Calling to instance method.  
  18.     }  
  19. }  
Static method

This belongs to the type, it does not belong to instance of the type. You can access static methods by class name.
 
You can put static method in static or non- static classes.

The only difference is that static methods in a non-static class cannot be extension methods.

Static often improves performance.
  1. public static class StaticClass  
  2. {  
  3.     public static int DoSomething()  
  4.     {  
  5.         return 2;  
  6.     }  
  7. }  
  8. public class Program  
  9. {  
  10.     static void Main()  
  11.     {  
  12.         StaticClass.DoSomething();  
  13.     }  
  14. }  

Up Next
    Ebook Download
    View all
    Learn
    View all