What does “this” mean in a static method declaration?

前端 未结 3 1427
傲寒
傲寒 2021-01-18 08:30

I\'ve seen some code that uses the keyword this in the function parameter declaration. For example:

public static Object SomeMethod( this Object         


        
3条回答
  •  暖寄归人
    2021-01-18 09:13

    It is how you declare an extension method.

    This means that you can invoke SomeMethod with .SomeMethod for any object. The object before the . will be the blah parameter.

    string s = "sdfsd";
    Object result = s.SomeMethod(false);
    

    The extension method will be available on all types inheriting from the type of the this parameter, in this case object. If you have SomeMethod(this IEnumerable enumerable) it will be available on all IEnumerable:s like List.

提交回复
热议问题