User Name:


User Email:




This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)
  

crazydeveloper Extension Methods in C#

Extension is great feature of C# 3.0. It's allow you to extend existing type class & Interface with new functionality, without having to sub-class or recompile the old type.

So we can say that Extension methods help us to add new methods to existing types without modifying the original code, inheriting or aggregating ....

It represents static methods as instance methods. An extension method uses the this-keyword in its parameter list. It must be located in a static class.

So question come in our mind that in which scenario we should use this functionality, cause normally we can put them together in a utility class and use, like this:

public class MyUtils

{

    public static bool IsNumeric(string s)

    {

           float output; return float.TryParse(s, out output);

     }

  }

Now you could check a string by executing a line of code like this:

string test = "4";
if (MyUtils.IsNumeric(test))
Console.WriteLine(
"Yes");
else

Console.WriteLine(
"No");

So when we use that , just think a scenario that you are working a project where you use a third party dll files. There some requirements come which need to update third party dll but you can do that so in this scenario you can use Extension methods funtionality like this :


public static class SampleExtensionMethodThirdParty

{

    public static bool IsNumeric(this thirdpartyclassname obj)

    {

       float output; return float.TryParse(obj, out output);

    }

}

 So, here without facing any problem you add new functionality with thirdparty dll.

However, in otherscenario with Extension Methods, you can actually extend the String class to support this directly. You do it by defining a static class, with a set of static methods that will be your library of extension methods. Here is an example:

public static class MyExtensionMethods
 {
 
public static bool IsNumeric(this string s)
{
float output; return float.TryParse(s, out output);
}
}

 Now, you can call the IsNumeric() method directly on strings, like this:

string test = "4";

if (test.IsNumeric())
Console.WriteLine(
"Yes");
else

Console.WriteLine(
"No");





Example with Interface:


using System;

namespace DefineDummyInterface
{
   

    public interface IDummyInterface
    {
        // Any class that implements IDummyInterface must define a method
        // that matches the following signature.
        void DummyMethod();
    }
}

// Define extension methods for IDummyInterface.

using System;
using DefineDummyInterface;

namespace DummyExtensions
{
   

    // The following extension methods can be accessed by instances of any  
    // class that implements IDummyInterface.
    public static class MyExtension
    {
        public static void Dummy(this IDummyInterface tInterface, int i)
        {
            Console.WriteLine
                ("Extension.Dummy(this IDummyInterface tInterface, int i)");
        }

        public static void Dummy(this IDummyInterface tInterface, string s)
        {
            Console.WriteLine
                ("Extension.Dummy(this IDummyInterface tInterface, string s)");
        }

       
    }
}

* An extension method will never be called if it has the same signature as a method defined in the type.

* Extension methos has low priority.

The only thing that separates this from any other static method, is the "this" keyword in the parameter section of the method. It tells the compiler that this is an Extension methods for the given class, and that's actually all you need to create an Extension methods.


crazydeveloper Home Page 06 May 2015

Become a Fan