c# extension method |
C# EXTENSION METHOD
“The mechanism of adding new method
in an existing class and also a structure without changing the source code of
original class is called extension method. In this process we don’t need any
permission of the original class to modify it”. This feature is not
available in visual version that less than 3.0. in c#.net the extension method
represent by normal method representation along with arrow down symbol.
Procedure to make an extension class
It is
necessary that you must have an existing class, and if it is sealed class or you
don’t want any changing in original class still want to add some new method in
it. In such scenario we use extension method in three simple steps.
Step1.
Create a new class, where the method will going to static so the class
will also be static, like this
Public static class <New-Class> ()
{
}
Step2. In this class write your method but note a point here all the method that you are going to
add should be static method.
Public static class <New-Class> ()
{
New static methods;
}
Step3. Bind your class to the existing class.
Using ‘this’ keyword we will bind a class to
another class like.
Public static class <New-Class> (this existing-class Obj)
{
New static methods;
}
Interesting point
Ques. To call the static method in main program first
we create instance of original class and then we call the existing as well as
our defined static method by using instance. But the point is that the static
method can’t call by instance it should call by class name then why the
confusion here?
Ans, The answer is our defined class will remain
static until we don’t bind with other class or structure but once we bond the
class with the original class our static class automatically changes into
non-static form static class or a method, that’s why we can call the method by
the instance of class.
Points to be noted
1. If we define an extension method that have same name and
signature of that existing method in original class then always the original
method will call, we can’t able to call
the extension method.
2. While creating new class we have taken a parameter like
Public static class
<New-Class> (this existing-class
Obj/parameter)
But at calling time we need not to
pass any value, because ‘this existing-class’ we use for binding not for taking
any parameter thus it’s not consider as parameter.
3. Referring point 2, how is it possible to take any parameter for
extension class then yes of course we take parameter, but it will define after
the binding code after colon. Like this
Public static class <New-Class> (this existing-class Obj, int a, double b)
Here the binding parameter code does not
consider as parameter but the rest will consider as parameter.
4. Method can be extension/add in a class using inheritance but in
this such as if a class is sealed then, so many problem in inheritance method thus
it is a weak process as compare extension method
EXAMPLE OF EXTENSION METHOD IN C#
By
following above method we will perform a small program that will having an
extension method.
Example 1: user defined class will modified in this
example with the help of extension method.
A class program() consist of two method that’s below
class Program
{
public void Triangle (int length, int height)
{
Console.WriteLine("Ares of
Triangle ={0}", (length * width) / 2);
}
public void Square (int length)
{
Console.WriteLine("Area of
Square =" + Math.Pow(length, 2));
}
static void Main(string[] args)
{
}
}
|
Extension method without modified
original in same project
static class ExtTest
{
public static void Rectangle (this Program p, int length, int width)
{
Console.WriteLine("Area of
Rectangle = {0}", length * width);
}
static void Main()
{
Program p = new Program();
p. Rectangle (12,
23);
p. Triangle (10,
11);
p. Square (12);
Console.ReadKey ();
}
}
Note: if you going to add in extension
method not in same project then you have to add reference also.
Example 2: Adding extension method in existing
structure Int32 ( ) in base library
Example 3: extension method in sealed class, we will
use string class because it is sealed class let see, if you want to see string
class go to definition of string.
Extension Proper case method in string sealed class
static class Program
{
public static void ToProper(this string str)
{
if (str.Trim().Length > 0)
string newstr = null;
str = str.ToLower();
string[] sarr =
str.Split();
foreach (string str2 in sarr)
{
char[] ch =
str2.ToCharArray();
ch[0] =
Char.ToUpper(ch[0]);
if (newstr == null)
newstr = new string(ch);
else
newstr += " " + new string(ch);
}
Console.WriteLine(newstr);
}
else
}
static void Main(string[] args)
{
string str;
str = "Hello WHo
aRE yOu";
str.ToProper();
Console.ReadKey();
}
}
|
0 comments:
Post a Comment
Please do not enter any spam link in the comment box