Ada*_*rth 23
听起来像扩展方法.
该@符号允许变量名称与C#关键字相同 - 我倾向于像瘟疫个人一样避免它们.
如果删除this关键字,它将不再是扩展方法,只是静态方法.根据调用代码语法,它可能不再编译,例如:
public static class IntegerMethods
{
public static int Add(this int i, int value)
{
return i + value;
}
}
int i = 0;
// This is an "extension method" call, and will only compile against extension methods.
i = i.Add(2);
// This is a standard static method call.
i = IntegerMethods.Add(i, 2);
Run Code Online (Sandbox Code Playgroud)
编译器将简单地将所有"扩展方法调用"转换为标准静态方法调用,但是扩展方法调用仍然只能根据this type name语法对有效的扩展方法起作用.
一些准则
这些是我自己的,但我觉得它们很有用.
System.Collections或者其他什么.不太有用但其他"常见"的东西倾向于Extensions.<namespace of extended type>使得可发现性至少通过惯例是一致的.MyFabulousExtensionMethod在object整个应用程序中出现.如果需要,可以将范围(命名空间)限制为非常具体,或绕过扩展方法并直接使用静态类 - 这些不会污染IntelliSense中的类型元数据.null(由于它们如何编译成静态方法调用)所以要小心并且不要假设"this"不为null(从调用端看起来这就像对null的成功方法调用目标).这些是可选的,并非详尽无遗,但我发现它们通常属于"好"建议的旗帜.因人而异.