C# Extension Method for Object

后端 未结 6 1902
萌比男神i
萌比男神i 2021-02-06 22:22

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 b

6条回答
  •  醉酒成梦
    2021-02-06 23:09

    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);
    

提交回复
热议问题