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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…