Use of “this” keyword in formal parameters for static methods in C#

前端 未结 6 1258
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 09:20

I\'ve come across several instances of C# code like the following:

public static int Foo(this MyClass arg)

I haven\'t been able to find an

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 09:37

    I just learnt this myself the other day: the this keyword defines that method has being an extension of the class that proceeds it. So for your example, MyClass will have a new extension method called Foo (which doesn't accept any parameter and returns an int; it can be used as with any other public method).

提交回复
热议问题