Extension Methods

Hello guys, in this article I would like to share some brief introduction of extension methods.
The agenda is how to create and use extension methods

Extension methods are called as if they were instance methods on extended type.

Example we have extended function like

string testString = string.Empty;
 testString.Split(',');
 testString.ToString();

and many more.

Extension methods are the customs method that are actually a static method, but can be used as instance methods.

Now lets jump over a simple demo, here I will create a new solution with name “Concepts” and add a console application with name “Concepts”.
To define an extensions method I will add new class library project “Extensions”. Inside that I will create a static function “ReverseText” that will reverse the order of input words.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Extensions
{
 public static class ExtentionSample
 {

 public static string ReverseText(this string text)
 {
 string[] array = text.Split(' ');
 Array.Reverse(array);

 string reverse = string.Empty;
 for (int indexCount = 0; indexCount < array.Length; indexCount++)
 {
 reverse = reverse +" "+ array[indexCount];
 
 }
 return reverse;
 }
 }
}

//ReverseText(this string text) if I remove “this” keyword I will get the error

“’string’ does not contain a definition for ‘ReverseText’ and no extension method ‘ReverseText’ accepting a first argument of type ‘string’ could be found (are you missing a using directive or an assembly reference?)”

Similarly, I will also create a method onlyWords that return count of Words used in a string

 public static int OnlyWords(this string value)
 {
 string[] array = value.Split(' ');
 
 return array.Length; 
 }

Now to Use this function I need to add the reference of Project “Extensions” to my project “Concepts”

The ReverseText Method can be called by using directive


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Extensions;//for extended methods

namespace Concepts
{
class Programs
 {
 
 static void Main(string[] args)
 {
 #region extention Method
 CheckExtentionMethods();
 #endregion
 Console.ReadKey(); 
 }

 private static void CheckExtentionMethods()
 {
 var testString = "hello friends this article is a brief introduction of Extended Methods";
 int count = testString.OnlyWords();
 Console.Write("String has {0} words ",count);
 Console.Write("\nReverse string is:{0}",testString.ReverseText());
 }

}

here it look like a custom extended Method but Intermediate language (IL) translate the code and call the static function.

Output of the above code is:
output

Reference :

MSDN library

Written By: Vipin Joshi, ASP.Net Developer, Mindfire Solutions

Advertisements

Posted on August 27, 2014, in C#, Uncategorized and tagged , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: