Why we use “this” in Extension Methods?

后端 未结 6 1259
遥遥无期
遥遥无期 2021-01-17 10:36

I want to ask why we use \"this\" keyword before the parameter in an extension method (C# Language)........... like this function :

    public static int ToI         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 11:10

    Mainly because it is how the C# spec defines an extension method. See Section 10.6.9

    10.6.9 Extension methods

    When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a pointer type.

提交回复
热议问题