Why do we put "this" before the parameter?

For ex:

public static Domain.Recruitment.Recruitment Map(this Data.Recruitment dv) { //some code return new Domain.Recruitment.Recruitment{} } 
+4
source share
3 answers

He marks the method as an “extension method,” which can be called as an instance method for an object and allows you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original Type:

+15
source

Adding the "this" parameter to the parameter creates an extension method. The extension method acts like an instance method, since you can call it on instances of the type of the first parameter in the extension method.

For example, the following is possible, since Map is marked as an extension method

 Data.RecruitMent dv = ...; dv.Map(); 
+3
source

This is an extension method. A way to expand a type without having to change it directly.

This is new in C # 3.0, although I believe

+2
source

Source: https://habr.com/ru/post/1300841/


All Articles