简体   繁体   English

可选参数和方法重载

[英]Optional parameters and method overloading

I've come across a library method with three parameters, all with default values: 我遇到了一个带有三个参数的库方法,所有参数都有默认值:

virtual M(bool b1 = false, string s1 = null, bool b2 = true)

Method M shouldn't have parameter s1 , so I want to remove it, but I don't want to make a breaking change in the DLL. 方法M不应该有参数s1 ,所以我想删除它,但我不想在DLL中进行重大更改。 Clients can obviously ignore s1 , but I don't want to leave it there because M can be overridden and parameter s1 is misleading. 客户端显然可以忽略s1 ,但我不想把它留在那里,因为M可以被覆盖,参数s1会产生误导。 So here was my attempt: 所以这是我的尝试:

virtual M(bool b1 = false, bool b2 = true)
[Obsolete] virtual M(bool b1, string s1, bool b2 = true)

I figured that since optional parameters are compiled into the call site, existing clients would carry on calling the method with three parameters, whereas new or recompiled clients not using s1 would link to the method with two parameters. 我认为,由于可选参数被编译到调用站点,现有客户端将继续使用三个参数调用该方法,而不使用s1新客户端或重新编译的客户端将使用两个参数链接到该方法。

Each call to M resolves okay, except this one: 每次对M调用都可以解决,除了这一个:

M(b2: false);

The compiler reports that the call is ambiguous between "M(bool, bool)" and "M(bool, string, bool)". 编译器报告“M(bool,bool)”和“M(bool,string,bool)”之间的调用不明确。

Oddly, in the parameter info (Ctrl+Shift+Space), Visual Studio is still showing the default values on the method with three parameters (despite cleaning and rebuilding, restarting VS, unloading and reloading projects). 奇怪的是,在参数信息(Ctrl + Shift + Space)中,Visual Studio仍然使用三个参数显示方法的默认值(尽管清理和重建,重新启动VS,卸载和重新加载项目)。

Obviously I can fix this by calling the new M something different, but I'm curious as to why it's not linking. 显然我可以通过调用新的M来解决这个问题,但我很好奇为什么它没有链接。 Should it (and something's just out of step, as the out-of-date parameter info suggests), or does the compiler have a genuine issue with this? 它应该(和某些东西一样不合时宜,如过时的参数信息所暗示的那样),还是编译器对此有真正的问题?

EDIT 编辑

Like @pswg and as per @JonSkeet's suggestion, I can't reproduce this in fresh code, so I guess the question becomes: is there anything else I can try other than rebuilding, restarting, reloading to force VS to relink this? 就像@pswg和@ JonSkeet的建议一样,我无法在新代码中重现这一点,所以我想问题就变成了:除了重建,重启,重新加载迫使VS重新链接之外,还有什么我可以尝试的吗?

Well this is embarrassing and interesting in equal measures (okay - maybe slightly more embarrassing than interesting). 嗯,这在同等程度上是令人尴尬和有趣的(好吧 - 也许比有趣的更令人尴尬)。

The compiler was correct (as it almost always is!). 编译器是正确的(因为它几乎总是!)。 I had assumed that the compiler error was referring to the two M methods I outlined in the question, but the client code with the error was also overriding the original M with three parameters, and still providing the defaults to all three parameters. 我假设编译器错误是指我在问题中概述的两个M方法,但带有错误的客户端代码也覆盖了具有三个参数的原始M ,并且仍然提供所有三个参数的默认值

I only discovered "the third M" when I actually added the [Obsolete] (sorry - I'd only posted it for illustration - didn't think that would be relevant) and got a warning about overriding an obsolete method. 当我实际添加[Obsolete]时,我才发现“第三个M”(对不起 - 我只是为了说明而张贴它 - 不认为这是相关的)并且有一个关于覆盖过时方法的警告。

I think this probably supports @pswg's comment! 我想这可能支持@ pswg的评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM