Extension methods are the best way to extend the functionality of existing types, without modifying existing types or creating subtypes from it. In this tutorial, I'll show you how to work with extension methods in C#.
For example you can easily create a method to check Even numbers from an existing int type
public static class IntExtension
{
public static bool IsEven(this int value)
{
if (value % 2 == 0)
return true;
return false;
}
}
public static class Program
{
public static void Main()
{
int value = 7;
bool valid = value.IsEven();
Console.WriteLine(value);
}
}