Extending Base Types with extension methods

Share

In almost every project there is need for code that converts one object type to another or provides custom formatting for output. Until C# 3.0 this was usually done by creating a wrapper class with static methods or worst by writing inline code that made those transformations on the spot.

With C# 3.0 Microsoft introduced a new language feature named extension methods. This permits a developer to associate a method to a base class and call it as if it is one of its built-in methods.

A simple example is the need for a method that is converting an integer that represents duration in seconds to a friendly format of hours and minutes for displaying it on web page.

public static class Converters
{
public static string ToHM(int i)
{
return (i / 60).ToString() + “:” + Math.Abs(i % 60).ToString().PadLeft(2, ‘0’);
}
}

This would then simply be called in code using

class Program
{
static void Main(string[] args)
{
int duration = 3003;
string result = Converters.ToHM(duration);
Console.WriteLine(“The result is: ” + result);
}
}

The result would be for this particular case “50:03”. Using a negative value like -3003 would output “-50:03”.
Using extension methods would transform this code like bellow.

public static class Converters
{
public static string ToHM(this int i)
{
return (i / 60).ToString() + “:” + Math.Abs(i % 60).ToString().PadLeft(2, ‘0’);
}
}
class Program
{
static void Main(string[] args)
{
int duration = 0;
string result = duration.ToHM();
Console.WriteLine(“The result is: ” + result);
}
}

The result would be the same, but we can use a simpler syntax and call the extension methods directly from the duration variable which can be of great help in big applications.

Please bear in mind that the extension methods are functionality equivalent to wrapper classes and work under the same limitations. They can access only public members of the types it is working with and can not read protected members (this is possible only using inheritance). In order to use extension methods you must import the appropriate namespace into your code ( using …) and be sure not to use the signature of a method defined in the type because in this case the method will never be called (by example .ToString()).

Bellow are some other simple examples of using extension methods:

public static string[] ToStringArray(this int[] intArray)
{
string[] stringArray = Array.ConvertAll(intArray, new Converter(ConvertIntToString));
return stringArray;
}
public static string ToCommaString(this int[] intArray)
{
string[] stringArray = ToStringArray(intArray);
return string.Join(“,”, stringArray);
}
public static StringCollection ToStringCollection(this int[] intArray)
{
string[] stringArray = ToStringArray(intArray);
StringCollection result = new StringCollection();
result.AddRange(ToStringArray(intArray));
return result;
}

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close