c# types of methods
// Here is a list of Method types in C#.
// 1. Pure virtual method.
// 2. Virtual method.
// 3. Abstract method.
// 4. Partial method.
// 5. Extension method.
// 6. Instance method.
// 7. Static method.
/* 1. 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();
/* 2. 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;
}
}
/* 3. 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);
/* 4. 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);
}
// This part can be in a separate file.
partial class A
{
// Comment out this method and the program
// will still compile.
partial void OnSomethingHappened(String s)
{
Console.WriteLine("Something happened: {0}", s);
}
}
}
/* 5. 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:
(i) For making Extension methods, you need to have a static class with
static method.
(ii) 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)
{
//
// Uppercase the first letter in the string.
//
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()
{
//
// Use the string extension method on this value.
//
string value = "deeksha sharma";
value = value.UppercaseFirstLetter();
Console.WriteLine(value);
}
}
/* 6. Instance method:
An instance method operates on a given instance of a class, and that
instance can be accessed as this.
Example: */
public class PropertyUtil
{
public void DoSomething()
{
// Do Something.
}
public void DoSomethingElse()
{
this.DoSomething(); // Calling to instance method.
}
}
public class Program
{
static void Main()
{
PropertyUtil util = new PropertyUtil();
util.DoSomething(); // Calling to instance method.
}
}
/* 7. 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.
Example: */
public static class StaticClass
{
public static int DoSomething()
{
return 2;
}
}
public class Program
{
static void Main()
{
StaticClass.DoSomething();
}
}