Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone


Microsoft .NET Framework

Microsoft .NET Framework

C# is my favourite programming and I’ve been doing C# programming for more than five years now. It is the most versatile and flexible language I found so far. With each new version it comes with many language features to support less and less coding and also to improve the readability of the code. I’m planning to introduce these features one by one in ‘Understanding C# Programming’ tutorial series. This will not be a beginner series as there are so many places you can find if needed. My motive is to show you some distinct feature of the language and how I used them in my projects. In this part will go through Extension Methods.

What is Extension Method

.NET framework or any other third party libraries come with many classes and many methods to support different functionality. However sometimes you might need to add certain repetitive functionality to existing class, this functionality may be specific to your project or it might be an obvious feature that nice to have in certain class which is missing. In certain case this might be a repetitive code segment in our project. I will explain few real world examples later. I found that there are  two major occasions that we need this. One is to enhance the functionality of existing class and then we need to class hierarchy to different class hierarchy (ex: business class tree to DTO class tree and vice versa . So what are the different options we have to implement this.

  1. Create separate static class and static method and do this repetitive logic there by passing all required parameters
    • We have to pass many parameters.
    • It doesn’t give good readability especially when the required method is more specific to a class
  2. If this feature is more specific and nice to have feature of existing class, we could derive a class from that and include it and use new class everywhere instead of original class
    • Too many levels of class inheritance
    • Just because of few functions we use our own class instead of intended class which may hinder the code readability

Extension method is a certain syntax provided by C# language that we can define new method and access it as a instance method of original intended class. But once it is compiled it is a static method which exists totally outside of the original class (Type)

You will understand this when we go through following example step by step.

I’m tired of writing string.Format()

I know most of you have come across string.Format() if you have ever done C# programming. How many times we have to write string.Format and then the formatting string with parameters. Let’s say I need to output following

“Error No: 100, Category: System”

What we normally do is following.

How to use Extension Method

Now let’s see how we could use extension method to extend the string type to do this easily. In this example you may not see immediate benefit, but believe me if you work in large project you might find this is very useful.

What we have done here is to define a static class called StringExtension  in MyExntensionMethods namespace and create a static method called Fmt. You can use whatever name you need, you can even use Format as the name but I wanted make it clear. First parameter is always of same type what we’re going to extend by this new method. Also make sure to include this keyword in front of the type. This is what tells the compiler that this is an extension method to string type. Then you can include any number of parameters you need to pass into this method. In this case I use same signature as in original string.Format() method. As you can see we do the same logic inside the method as before. But what is the purpose of this? you will see it next when we use this new extension method in following code.

Make sure to include the namespace where ever you use this new extension method. See that now you can simply use the method right after the format string and pass required parameters. (because format string also of string type).

How about few more examples

Glad you asked let’s add few more extension methods to string type. Let’s say you access remote web service or REST service and you receive certain data structure but all values are received as string. There could be so many such values, think how much effort you have to put converting each and assign to proper types. Now look at following two extension methods written to convert to  int and Guid types, also note that if you need you can even do any validation and return predefined values to represent invalid data.

This how you can use it

I think now you can understand the use of extension methods and we will discuss more examples and real world usage in next few tutorials.


Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone