在这两种链扩展方法中,是否有理由使用其中一种而不是另一种?

发布于 2025-01-03 18:24:32 字数 850 浏览 1 评论 0原文

假设我有一个具有以下两个签名的重载扩展方法:

public static void MyExtensionMethod(this Foo foo);
public static void MyExtensionMethod(this Foo foo, Bar bar);

我想将一个方法链接到另一个方法。我可以通过以下两种方式之一来完成此操作:

链接技术#1

public static void MyExtensionMethod(this Foo foo)
{
    // Call overload using extension method syntax.
    foo.MyExtensionMethod(new Bar());
}

public static void MyExtensionMethod(this Foo foo, Bar bar)
{
    // Do stuff...
}

链接技术#2

public static void MyExtensionMethod(this Foo foo)
{
    // Call overload as a regular method.
    MyExtensionMethod(foo, new Bar());
}

public static void MyExtensionMethod(this Foo foo, Bar bar)
{
    // Do stuff...
}

这是我的问题:将重载方法作为扩展方法调用之间有什么区别吗与常规方法相比?如果是这样,有什么区别?其中一个比另一个更可取吗?

Say I have an overloaded extension method with the following two signatures:

public static void MyExtensionMethod(this Foo foo);
public static void MyExtensionMethod(this Foo foo, Bar bar);

I'd like to chain one method to the other. I can do this in one of two ways:

Chaining Technique #1

public static void MyExtensionMethod(this Foo foo)
{
    // Call overload using extension method syntax.
    foo.MyExtensionMethod(new Bar());
}

public static void MyExtensionMethod(this Foo foo, Bar bar)
{
    // Do stuff...
}

Chaining Technique #2

public static void MyExtensionMethod(this Foo foo)
{
    // Call overload as a regular method.
    MyExtensionMethod(foo, new Bar());
}

public static void MyExtensionMethod(this Foo foo, Bar bar)
{
    // Do stuff...
}

This is my question: Is there any difference between calling the overloaded method as an extension method versus as a regular method? If so, what's the difference? Is one preferable to the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拿命拼未来 2025-01-10 18:24:32

假设编译器能够解决所有问题,它们应该作为等效的 MSIL 发出。扩展方法只是编译器技巧;它们实际上是静态方法。

Servy 在他的回答中指出,如果您有一个同名的实例方法,您可能会遇到问题。 我同意...因此,强烈建议您永远不要将扩展方法命名为与任何实例方法相同的名称。

Assuming the compiler is able to resolve everything, they should be emitted as equivalent MSIL. Extension methods are just compiler tricks; they are static methods in reality.

Servy notes in his answer that if you have an instance method with the same name, you can run into problems. I agree... and as a result, strongly urge you never to name an extension method the same as any instance method.

陌路黄昏 2025-01-10 18:24:32

这个问题相当于问“将扩展方法调用为扩展与静态方法之间有区别吗?”。答案是否定的。编译器将这两个调用转换为相同的代码。

This question is equivalent to asking "is there a difference between calling an extension method as an extension versus as a static method?". The answer is no. The compiler translates both calls to the same code.

季末如歌 2025-01-10 18:24:32

我发现你的技巧 2 读起来更自然。

第一个有点刺耳 - 我需要弄清楚它调用了第二个扩展方法。

因此,仅就可读性而言,我会选择技术 2。

就如何调用事物而言,扩展方法只是调用静态方法的语法糖。这两个例子的意思是一样的。


有一个例外,正如 @Servy 的答案中所述 - 如果您有一个具有完全相同名称和参数的实例方法(除了第一个 this 参数),您可能会遇到问题。虽然在您自己的代码库中不太可能发生,但这种情况可能会发生 - 只是使用扩展方法的结果。

这样做是不好的做法 - 不要将实例方法命名为与扩展方法相同。

I find your technique 2 to be more natural to read.

The first one is a bit jarring - I need to figure out that it calls a second extension method.

So, on readability alone, I would select technique 2.

In terms of how things get called - extension methods are just syntactic sugar for calls to static methods. Both examples amount to the same thing.


There is one exception, as noted in the answer by @Servy - if you have an instance method with the exact same name and parameters (bar the first this parameter), you can have issues. Though unlikely to occur in your own code base, this may happen - just a consequence of using extension methods.

It is bad practice to do this - don't name instance methods the same as extension methods.

荭秂 2025-01-10 18:24:32

其实第二个更稳定。如果我执行以下操作,第一个将被更改:

public class Foo
{
    public void MyExtensionMethod(Bar bar)
    {
        Console.WriteLine("instance method");
    }
}

如果我添加这个,那么在第一种情况下它调用实例方法,在第二种情况下它调用扩展方法。

The second one is actually more stable. The first one will be changed if I do something like:

public class Foo
{
    public void MyExtensionMethod(Bar bar)
    {
        Console.WriteLine("instance method");
    }
}

If I add this then in the first case it calls the instance method, and in the second case it calls the extension method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文