Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension Method Performance

            /*I have defined Extension Methods for the TypeX like this*/ 
        public static Int32 GetValueAsInt(this TypeX oValue)
        {
            return Int32.Parse(oValue.ToString());
        }
        public static Boolean GetValueAsBoolean(this TypeX oValue)
        {
            return Boolean.Parse(oValue.ToString());
        }


         TypeX x = new TypeX("1");
         TypeX y = new TypeX("true");


         //Method #1
         Int32 iXValue = x.GetValueAsInt();
         Boolean iYValue = y.GetValueAsBoolean();

         //Method #2
         Int32 iXValueDirect = Int32.Parse(x.ToString());
         Boolean iYValueDirect = Boolean.Parse(y.ToString());

Dont get carried away by TypeX saying that I should define those methods inside TypeX rather than the Extension) I have no control on it (Actual Class I defined it is on SPListItem.

I wanted to convert the TypeX to Int or Boolean and this operation is one Common thing that I am doing in lots of Places in the code. I wanted to know will this cause performance degrade.I tried to interpret IL code using Reflector, but am not good at it. May be for the above example there wont be any performance degrade. In general I wanted to know about the implications with Regard to the Performance while using the Extension methods.

like image 473
Kusek Avatar asked Jun 17 '09 11:06

Kusek


People also ask

What is an advantage of using extension methods?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

Are extension methods good?

For an application programmer, extension methods are an incredibly powerful and expressive tool. They enable convenience, extensibility, and an improved intellisence experience. However, many of the features that make extension methods so useful for library consumers can be problematic for class library authors.

When should we use extension methods in C#?

Use extension method which would help you to add a method to existing types without modifying the original source code and without the use of inheritance. Use 'this' keyword in extension method parameter so that it refer to OriginalClass class when you invoke it in Program class.

What is the difference between a static method and an extension method?

The only difference between a regular static method and an extension method is that the first parameter of the extension method specifies the type that it is going to operator on, preceded by the this keyword.


1 Answers

Extension methods are just a compile-time change from:

x.GetValueAsBoolean()

to

Extensions.GetValueAsBoolean(x)

That's all that's involved - translating what looks like an instance method call into a call to a static method.

If you don't have performance problems with the static method, then making it an extension method won't introduce any new problems.

EDIT: IL, as requested...

Taking this sample:

using System;

public static class Extensions
{
    public static void Dump(this string x)
    {
        Console.WriteLine(x);
    }
}

class Test
{
    static void Extension()
    {
        "test".Dump();
    }

    static void Normal()
    {
        Extensions.Dump("test");
    }
}

Here's the IL for Extension and Normal:

.method private hidebysig static void  Extension() cil managed
{
  // Code size       13 (0xd)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldstr      "test"
  IL_0006:  call       void Extensions::Dump(string)
  IL_000b:  nop
  IL_000c:  ret
} // end of method Test::Extension

.method private hidebysig static void  Normal() cil managed
{
  // Code size       13 (0xd)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldstr      "test"
  IL_0006:  call       void Extensions::Dump(string)
  IL_000b:  nop
  IL_000c:  ret
} // end of method Test::Normal

As you can see, they're exactly the same.

like image 84
Jon Skeet Avatar answered Nov 08 '22 10:11

Jon Skeet