C# Extension Method for Object

Mandrake picture Mandrake · Feb 4, 2011 · Viewed 25.3k times · Source

Is it a good idea to use an extension method on the Object class?

I was wondering if by registering this method if you were incurring a performance penalty as it would be loaded on every object that was loaded in the context.

Answer

Andrew Bezzub picture Andrew Bezzub · Feb 4, 2011

In addition to another answers:

there would be no performance penalty because extension methods is compiler feature. Consider following code:

public static class MyExtensions
{
    public static void MyMethod(this object) { ... }
} 

object obj = new object();
obj.MyMethod();

The call to MyMethod will be actually compiled to:

MyExtensions.MyMethod(obj);