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

前端 未结 6 1266
隐瞒了意图╮
隐瞒了意图╮ 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 10:02

    "this" extends the next class in the parameter list

    So in the method signature below "this" extends "String". Line is passed to the function as a normal argument to the method. public static string[] SplitCsvLine(this String line)

    In the above example "this" class is extending the built in "String" class.

提交回复
热议问题