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#.
- Pure virtual method.
- Virtual method.
- Abstract method.
- Partial method.
- Extension method.
- Instance method.
- 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:
- 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:
- public class A
- {
- public virtual int Calculate(int a, int b)
- {
- return a + b;
- }
- }
- public class B: A
- {
- public override int Calculate(int a, int b)
- {
- return a + b + 1;
- }
- }
Abstract Method Abstract Method is the method with no implementation and is implicitly virtual. You can make abstract method only in Abstract class.
Example:
- 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: - namespace PM
- {
- partial class A
- {
- partial void OnSomethingHappened(string s);
- }
-
- partial class A
- {
-
-
- partial void OnSomethingHappened(String s)
- {
- Console.WriteLine("Something happened: {0}", s);
- }
- }
- }
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 - For making Extension methods, you need to have a static class with static method.
- Do not make Extension method for one or two lines of code, write Extension method for logic.
E.g.
- public static class ExtensionMethods
- {
- public static string UppercaseFirstLetter(this string value)
- {
-
-
-
- if (value.Length > 0)
- {
- char[] array = value.ToCharArray();
- array[0] = char.ToUpper(array[0]);
- return new string(array);
- }
- return value;
- }
- }
- class Program
- {
- static void Main()
- {
-
-
-
- string value = "deeksha sharma";
- value = value.UppercaseFirstLetter();
- Console.WriteLine(value);
- }
- }
Instance method An instance method operates on a given instance of a class, and that instance can be accessed as this.
- public class PropertyUtil
- {
- public void DoSomething()
- {
-
- }
- public void DoSomethingElse()
- {
- this.DoSomething();
- }
- }
- public class Program
- {
- static void Main()
- {
- PropertyUtil util = new PropertyUtil();
- util.DoSomething();
- }
- }
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.
- public static class StaticClass
- {
- public static int DoSomething()
- {
- return 2;
- }
- }
- public class Program
- {
- static void Main()
- {
- StaticClass.DoSomething();
- }
- }