SIGN UP MEMBER LOGIN:    
ARTICLE

Extension Methods in C#

Posted by Vipul Kelkar Articles | Visual C# October 17, 2011
.NET framework comes with a set of inbuilt classes and types which we use extensively in our applications. Integer, string, float etc are some of these which are very frequently used.
Reader Level:

The .NET framework comes with a set of inbuilt classes and types which we use extensively in our applications. Integer, string, float etc are some of these which are very frequently used. These come with a set of predefined methods and properties. Lets say we declare a string variable in our program, we can use a list of methods that are provided by the .NET framework within it.

ExtMethd1.gif

Suppose we have to execute a custom method on any string object for a number of times in our code. Wouldn't it be great if we could just execute it as any_string_object.ourCustomMethod().

Extension methods help us achieve
 exactly this. Without using Inheritance, we can extend the existing classes to add our own methods.

Lets say we have a integer and we need to check : if the interger is <100, the integer is divisible by 2. We'll write an extension method for this.

The extension method is written inside a static class and the method is also defined as static. Though it is defined as a static method it is still invoked on a particular instance. Below is the code for the extension method isAllowed()

ExtMethd2.gif

The "this" keyword tells that the method should be associated with the type specified ahead of it. In this case it is INT
Here the extension method is invoked on the variable that we have read through the console. We can use this method on any other integer variable in the program.

ExtMethd3.gif

The result of the program is :

ExtMethd4.gif

Extension methods cannot be used to override existing methods and these can be applied only to add extension methods. We cannot create properties using the above concept.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Lucy, glad you liked the article. I would say that this is the specification, but per my understanding : We define the class to be static as the static class cannot be instantiated and the members of the class would also be static. So when we are calling an extension method, we are not creating an instance of a class but we call extension method using an object of type speficied front of "this" keyword in method parameters. In this case it is INT. Being a static method, extension methods are mainly used as helper methods and can be called on any object of the type for which it is written.

Posted by Vipul Kelkar Oct 18, 2011

Hi Vipul, it was a nice article, can u tell me why we put extension method definiton inside the Static class. Thanks in advance

Posted by Lucy Jane Oct 18, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor