What is Extension Method?

Posted by hpu12345 under C# on 5/26/2011 | Points: 10 | Views : 1107 | Status : [Member] | Replies : 3
Extension methods are a feature of C# 3.0.Extension method allow you to add new method to the existing class without modifying the code ,recompiling or modifying the original code.you can write extension methods for interfaces/abstract class.Extension method is nothing but the Visitor pattern.
Visitor Pattern:-
Visitor pattern allows us to change the class structure without changing the actual class. Its way of separating the logic and algorithm from the current data structure. Due to this you can add new logic to the current data structure without altering the structure. Second you can alter the structure without touching the logic

To write an extension method, you do the following

Define a public static class.
Define a public static method in the class where the first argument is the data type for which you want the extension method
Use the this keyword on the first argument to your public static method. The this keyword denotes the method as an extension method


Example 1


using System;
public class MyClass
{
public int IntField;
public MyClass(int arg)
{
IntField = arg;
}
}

public static class MyClassExtension
{
public static void PrintIt(this MyClass arg)
{
Console.WriteLine("IntField:{0}", arg.IntField);
}
public static void Main()
{
MyClass mc = new MyClass(10);
mc.PrintIt();
Console.ReadKey();

}
}
Example 2
using System;
namespace ExtensionMethods
{
public static class ExtensionsClass
{
public static string Reverse(this String strReverse)
{
char[] charArray = new char[strReverse.Length];
int len = strReverse.Length - 1;
for (int i = 0; i <= len; i++)
{
charArray[i] = strReverse[len - i];
}
return new string(charArray);
}
public static void Main()
{
string str = "Hi How r u?";
String strss= str.Reverse();

}
}
}




Responses

Posted by: Susanthampy on: 5/26/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Nice post....



Regards,
Susan

hpu12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 5/27/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Nice one.
But kindly post your information in our code section (or) article section its really useful to others.

Thanks,
T.Saravanan

hpu12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Maheshbabu on: 1/17/2014 [Member] Starter | Points: 25

Up
0
Down
Hi,
Extension Method:
An extension method is a static method of a static class that can be invoked using the instance method syntax. Extension methods are used to add new behaviors to an existing type without altering. In extension method "this" keyword is used with the first parameter and the type of the first parameter will be the type that is extended by extension method.
Conceptually, extension methods provides as an implementation of the decorator structural pattern.


For complete code regarding extension method..please visit.
http://www.dotnetchallengers.com/App_Folder/Content/Articles/C-Sharp/C%20Sharp%20Extension%20method%20C-Sharp/C_Sharp_Extension_method_C-Sharp.aspx?TutorialIds=10

hpu12345, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response