我正在阅读有关扩展方法的内容,并与他们进行一起讨论,看看它们是如何工作的,我试过这个:
namespace clunk {
public static class oog {
public static int doubleMe(this int x) {
return 2 * x;
}
}
class Program {
static void Main() {
Console.WriteLine(5.doubleMe());
}
}
}
Run Code Online (Sandbox Code Playgroud)
并按预期工作,使用doubleMe方法成功扩展int,打印10.
接下来,作为一个老C家伙,我想知道我是否可以这样做:
namespace clunk {
public static class BoolLikeC {
public static bool operator true(this int i) { return i != 0; }
public static bool operator false(this int i) { return i == 0; }
}
class Program {
static void Main() {
if ( 7 …Run Code Online (Sandbox Code Playgroud)