这些默认参数的人造模拟之间是否存在显着的机械差异?

发布于 2024-09-03 05:05:31 字数 912 浏览 3 评论 0原文

C#4.0 引入了一个非常奇特且有用的东西,允许方法中使用默认参数。但 C#3.0 没有。因此,如果我想模拟“默认参数”,我必须创建两个该方法,一个包含这些参数,另一个不包含这些参数。我有两种方法可以做到这一点。

版本 A - 调用其他方法

public string CutBetween(string str, string left, string right, bool inclusive)
{
    return str.CutAfter(left, inclusive).CutBefore(right, inclusive);
}

public string CutBetween(string str, string left, string right)
{
    return CutBetween(str, left, right, false);
}

版本 B - 复制方法主体

public string CutBetween(string str, string left, string right, bool inclusive)
{
    return str.CutAfter(left, inclusive).CutBefore(right, inclusive);
}

public string CutBetween(string str, string left, string right)
{
    return str.CutAfter(left, false).CutBefore(right, false);
}

这些之间有什么真正的区别吗?这不是关于优化或资源使用或任何东西的问题(尽管其中一部分是我保持一致的总体目标),我什至不认为选择一种方法或另一种方法有任何重大影响,但我发现它询问这些事情比错误地假设更明智。

C#4.0 introduced a very fancy and useful thing by allowing default parameters in methods. But C#3.0 doesn't. So if I want to simulate "default parameters", I have to create two of that method, one with those arguments and one without those arguments. There are two ways I could do this.

Version A - Call the other method

public string CutBetween(string str, string left, string right, bool inclusive)
{
    return str.CutAfter(left, inclusive).CutBefore(right, inclusive);
}

public string CutBetween(string str, string left, string right)
{
    return CutBetween(str, left, right, false);
}

Version B - Copy the method body

public string CutBetween(string str, string left, string right, bool inclusive)
{
    return str.CutAfter(left, inclusive).CutBefore(right, inclusive);
}

public string CutBetween(string str, string left, string right)
{
    return str.CutAfter(left, false).CutBefore(right, false);
}

Is there any real difference between these? This isn't a question about optimization or resource usage or anything (though part of it is my general goal of remaining consistent), I don't even think there is any significant effect in picking one method or the other, but I find it wiser to ask about these things than perchance faultily assume.

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

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

发布评论

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

评论(3

执手闯天涯 2024-09-10 05:05:31

唯一真正的区别在于维护;第二个版本本质上是代码重复的一种形式,如果您需要更改这些版本的实现,您将需要做更多的工作(可能还需要运行更多的测试)。

否则,它们在其他方面基本相同 - 在第一种情况下,您将在调用堆栈上有一个额外的方法,这不会对性能或资源使用产生任何明显的影响(正如您所说,不是优化)问题)。

鉴于此,当我需要同一方法的多个重载时,我倾向于执行第一个示例中所做的操作 - 让多个重载方法都调用相同的“通用”方法。

The only real difference is that of maintenance; the second version is essentially a form of code duplication and you'll have more work to do (and perhaps more tests to run) if you need to change the implementation of these.

Otherwise, they're basically the same in every other respect - you'll have one extra method on the call stack in the first case, which won't have any noticeable effect on performance or resource usage (as you said, not an optimization problem).

Given that, when I need several overloads of the same method I tend to do what you did in the first example - have several overloaded methods all call the same "general" method.

烈酒灼喉 2024-09-10 05:05:31

行为不会有任何差异,不会 - 但版本 B 显然并不只是应用默认值。您需要仔细阅读代码以验证是否存在细微的差异。我认为最好通过一个包含真实逻辑的“主”方法来汇集任何默认值。这也使得以后更改逻辑变得更容易 - 您只需在一处进行即可。

There'll be no difference in behaviour, no - but version B isn't obviously just applying a default. You need to read the code carefully to verify that there aren't subtle differences. I think it's better to funnel any default values through one "master" method containing the real logic. That also makes it easier to change the logic later - you only have to do it in one place.

混吃等死 2024-09-10 05:05:31

一个区别是,如果第一个版本中的 CutAfterCutBefore 签名因任何原因发生更改,您只需更新一行,而在第二个版本中,您必须更新尽可能多的行行,因为你有方法。

One difference is that if the CutAfter or CutBefore signature changes for any reason in the first version you need only update a single line whereas in the second version you have to update as many lines as you have methods.

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