Translate

Monday 2 June 2014

Extension Methods in .NET

Why Extension Method: Another question is, why do we need to use the extension methods, we can create our own methods in client application or in other class library and we can call those methods from the client application but the important point is, these methods would not be the part of that class (class of the third party dll). So adding the extension methods in the class, you are indirectly including another behavior in that class. I am talking about the class which is the part of the third party dll (Most Important).

So when you create your own extension methods for specific types those methods would be accessible for that type.

This can be easily understood by the following diagram:

ExMtd1.gif

(I am object of the class)

After some time, client application wants to add another behavior in that class; let's say 'talk' then extension methods can be used to add this extra behavior in that class without altering any code.

ExMtd2.gif

Implementation: The following is the structure of the third party class, which is not accessible to the developer. This class contains a method 'Walk'.
public class Class1{
    public String Walk()
    {
        return "I can Walk";    }}
Now it's time to create the Extension Method. Just use the following steps:

Step 1: Create the Static Class

Step 2: Create the Static Method and give any logical name to it. 

Step 3: In the Extension Method Parameter, just pass this along with the object type and 
the object name as given below:

ExMtd3.gif

The client application can access both the method "Walk" and the extension method "TalkExtMethod":
Class1 obj = new Class1 ();
Obj.TalkExtMethod ()  
obj.Walk ()


Benefits: There may be various benefits of using the Extension Methods in your code. Let's say you are using the method1 () of the class in the third party dll and the object instantiation has failed due to another reason; then you will get the object reference error while calling the method1 () if you did not handle the null reference check.
Class1 obj = GetClass1Object ();//If the obj is null, you will get the object reference errorobj.Method1();

Although the following code is safer than previous one but the problem is you will have to include this check every time when you call the Method1 and if the developer forgets to include this check in the code then the program throws an object reference error.
Class1 obj = GetClass1Object ();if (obj!= null)
{
    obj.Method1 ();
}

If we use the extension method to solve this problem we get outstanding results. Instead of checking the null reference every time in the code we handle it in the extension methods which is called by the client application.
public static string Method1ExtMethod(this Class1 objClass)
{
    //If the obj is not null, then call the Method1()
    if (objClass! = null)
    {
        return objClass.Method1 ();
    }
    return string.Empty;    
}
Class1 obj = GetClass1Object ();//Don't include any check whether the object is null or notobj.Method1ExtMethod()

What is Extension Methods


1.Extension methods allow an existing type to be extended with new methods without altering the definition of the original type. 
2.An extension method is a static method of a static class, where the this modifier is applied to the first parameter. 
3.The type of the first parameter will be the type that is extended.

Example:


public static class StringHelper
{
public static bool IsCapitalized (this string s)
{
if (string.IsNullOrEmpty(s)) return false;
return char.IsUpper (s[0]);
}
}



4.The IsCapitalized extension method can be called as though it were an instance method on a string.

String s = "Perth“;
Console.WriteLine (s.IsCapitalized());

5.An extension method call, when compiled, is translated back into an ordinary static method call.
Console.WriteLine (StringHelper.IsCapitalized (s));


Ambiguity and Resolution about Extension Methods


1.An extension method cannot be accessed unless the namespace is in scope.

2.Any compatible instance method will always take precedence over an extension method.

3. If two extension methods have the same signature, the extension method must be called as an ordinary static method to disambiguate the method to call. 

4.If one extension method has more specific arguments, however, the more specific method takes precedence.

Example :-

static class StringHelper
{
public static bool IsCapitalized (this string s) {...}
}
static class ObjectHelper
{
public static bool IsCapitalized (this object s) {...}
}

bool test1 = "Perth".IsCapitalized();
bool test2 = (ObjectHelper.IsCapitalized ("Perth"));


Using ClientIDMode Feature of ASP.net 4.0:


Using ClientIDMode Feature of ASP.net 4.0: 

The client ID mode feature of ASP.net 4.0 gives the developer a little bit more control over how the ID will be generated. By default all ASP.net controls uses the Inherit as theClientIDMode

Static Mode: 

As the name suggests the static mode can be used to give a constant ID to the ASP.net control. Simply assign ClientIDMode property equal to Static in your control and it will start generating the ID using the static mode. 

The implementation below shows how to use the ClientIDMode equal to static for the GridView control.

1<asp:GridView ID="gvCustomers" ClientIDMode="Static" runat="server">

Now run the application and examine the ID generated by the GridView control. The screenshot below shows that the GridView ID has no affect from the parent control ID.



Let's make things a little bit more interesting by adding a TemplateField column to our GridView control. The implementation is shown below: 

01<asp:GridView ID="gvCustomers" ClientIDMode="Static" runat="server">
02
03<Columns>
04<asp:TemplateField>
05<ItemTemplate>
06<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
07</ItemTemplate>
08</asp:TemplateField>
09</Columns>
10
11</asp:GridView>


As you can see in the code above we have added a new TextBox control inside the GridView control. Since the default mode of all ASP.net controls is set to inherit this will cause all TextBox controls to create the same ID as shown in the screenshot below: 



You might be expecting that the page will result in an error but no error is generated and you will have multiple controls having the same ID. This is not a good practice because it is now extremely hard to select a particular control from inside the GridView control. If you are using JQuery to select the "txtName" element using the ID then it will return you the first occurrence of the element with the ID "txtName". 

In the next section we are going to fix the problem of creating multiple controls with the same ID using the predictable option of the ClientIDMode property. 

Predictable Mode: 

Predictable mode allows to create IDs that are dependent on the ClientIDRowSuffix property of the parent control. If you do not provide a value for the ClientIDRowSuffix then the RowIndex will be used as ClientIDRowSuffix. Take a look at the implementation below where we have set the client ID mode property of the TextBox control to Predictable.

01<asp:GridView ID="gvCustomers" ClientIDMode="Static" runat="server">
02
03<Columns>
04<asp:TemplateField>
05<ItemTemplate>
06<asp:TextBox ID="txtName" ClientIDMode="Predictable" runat="server"></asp:TextBox>
07</ItemTemplate>
08</asp:TemplateField>
09</Columns>
10
11</asp:GridView>


If you run the above code and view the source of the page you will notice that the index number of the row is concatenated with the ID of the TextBox control. 



Although the above approach creates unique ID for the TextBox control inside the GridView control but they are still not developer friendly. For this reason you can assign a property from the data source to the ClientIDRowSuffix field. It is a good practice to use a unique ID as a value for the ClientIDRowSuffix so all the controls generated have unique IDs. In the code below we are using the CustomerID as the ClientIDRowSuffix which will be appended with the ID of the TextBox control.

01<asp:GridView ID="gvCustomers" ClientIDRowSuffix="CustomerId" ClientIDMode="Static" runat="server">
02
03<Columns>
04<asp:TemplateField>
05<ItemTemplate>
06<asp:TextBox ID="txtName" ClientIDMode="Predictable" runat="server"></asp:TextBox>
07</ItemTemplate>
08</asp:TemplateField>
09</Columns>
10
11</asp:GridView>

id=txtName_customerid from db

EX: txtName_1234