You must Sign In to post a response.
  • Category: ASP.NET

    Extension methods in c#

    Hi,

    what is extension method ...can any one explain me with
    clear example ... and what is the differnce between extension method and normal method
    with example....
  • #725163
    Hi.
    Extension methods were introduced in 3.5 framework version of Dot.net where we add methods to the class without changing the existing code of that class. There are used when we want to use the dlls of third party but need some changes in the existing method. So we create extension method.
    Please refer to the following link for more details:
    http://www.codeproject.com/Articles/261639/Extension-Methods-in-NET

    Thanks

    Reena

  • #725186
    Hi,

    Using extension method you will be able to add methods without creating the new derived type to an existing types. Rather than me going through the complete details I would like you to read MSDN article for complete details: msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx


    Regards,
    Asheej T K

  • #725257
    Extension methods are similar like other methods but it can be use to added classes without rebuilding or compiling .Syntax is given below
    Syntax:
    public static class Extensions
    {

    }

    public static <Return Type> <Function Name>(this <Object Type> <Variable Name>)
    {
    Write you requires
    .........
    ......
    return <Return Type> ;
    }

    Example of Extension Methods

    public static class Extensions
    {
    public static int ToInt32Ext(this string myvar)
    return Int32.Parse(s);
    }

    public static int ToInt32(string myvar)
    {
    return Int32.Parse(myvar);
    }

    }

    void Main()
    {
    string myvar = "7";
    int Evar = myvar.ToInt32Ext();
    Console.WriteLine(Evar);
    Evar = Extensions.ToInt32(myvar);
    Console.WriteLine(x);
    }


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.