Extension Methods in C#

Let say you want to extend a class that you cannot inherit. These classes might be defined as sealed or even a 3rd party DLL that you have just downloaded from nugget. How would you extend these classes?

Let us take an example of struct types in C# that are sealed by default, such as DateTime, String, Int32 etc..

You know that cannot extend DateTime as:

public struct CustomDateTime: DateTime    {    }
Error at compile time: Type CustomDateTime in interface list is not an interface

One option is to Wrap DateTime variable within CustomDateTime class and then provide your custom solutions. As an example:

public class CustomDateTime
 {
 private DateTime _dateTime;
public CustomDateTime()
 {
}
 public CustomDateTime(long ticks)
 {
 }
public CustomDateTime(long ticks, DateTimeKind kind)
 {
 }
public CustomDateTime(int year, int month, int day)
 {
 }
public CustomDateTime(int year, int month, int day, Calendar calendar)
 {
 }
public CustomDateTime(int year, int month, int day, int hour, int minute, int second)
 {
 }
public CustomDateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar)
 {
 }
public CustomDateTime(int year, int month, int day, int hour, int minute, int second, CustomDateTimeKind kind)
 {
 }
public CustomDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
 {
 }
public CustomDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond,
 Calendar calendar)
 {
 }
public CustomDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond,
 CustomDateTimeKind kind)
 {
 }
public CustomDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond,
 Calendar calendar, CustomDateTimeKind kind)
 {
 }
 }
 

As you can see here that you are actually reinventing a wheal.

You have an another, second, option to define static class such as:

public static class DateTimeUtility
 {
 public static string CustomFormat(DateTime date)
 {
 return "Your Date is: " + date.ToLongDateString();
 }
 }
public class Program
 {
 public static void Main()
 {
 DateTimeUtility.CustomFormat(new DateTime());
 }
 }

 

However, it is not user friendly because:

  • You have to pass variable
  • Not easy to read
  • You have to write more code. For e.g. DateTimeUtility.CustomFormat

 

Extension methods are not a necessity but it is an elegant way of writing code.

With the help of extension method the CustomFormat will become an instance method of DateTime.

The above code will look like as:

 

public static class DateTimeUtility    {
        public static string CustomFormat(this DateTime date)
        {
            return "Your Date is: " + date.ToLongDateString();
        }
    }

    public class Program
    {
        public static void Main()
        {
            var dateTime = new DateTime();
            dateTime.CustomFormat();
        }

Hence, you can write your method once with Template type and you will be able to use the same again and again.
An another example could be to implement “Between” check:

public static class ComparableExtensions
{
 public static bool WithRange<T>(this T actual, T lower, T upper) where T : IComparable<T>
 {
 return actual.CompareTo(lower) >= 0 && actual.CompareTo(upper) < 0;
 }
}
Var number = 5;
if (number. WithRange (3,7))
{
 // ....
}