简体   繁体   English

C#方法的区别仅在于可选参数

[英]C# Methods differ only by optional parameters

I have found this topic, but it's VB...and they get an error: vb issue 我找到了这个主题,但是它是VB ...,他们得到一个错误: vb问题

Here are my method signatures. 这是我的方法签名。 Notice one has a different return type. 请注意,其中一个具有不同的返回类型。

public static bool PopulateRunWithSimpleValueByFieldIdSync(string fieldValue, string fieldId, IViewModel myself, int index)

VS VS

public static void PopulateRunWithSimpleValueByFieldIdSync(string fieldValue, string fieldId, IViewModel myself, int index = 0, PopulateDoneCallback populateDone = null)

The actual call I was making: 我实际拨打的电话是:

PopulateRunWithSimpleValueByFieldIdSync(date, dtx.DateField, saver, index);

The compiler decided to pick the first method, and not give me an error. 编译器决定选择第一种方法,并且没有给我错误。 Once the first method was removed (it was unused code), it started calling the second method. 一旦删除了第一个方法(它是未使用的代码),它将开始调用第二个方法。

Is there an option somewhere to treat this as an error? 某处是否有将其视为错误的选项?

如果您希望在编译时进行标记,则需要使用某种形式的第三方代码分析,因为C#语言规范将当前行为定义为应该发生的事情。

This is per design, according to the specs 这是根据设计,根据规格

Use of named and optional arguments affects overload resolution in the following ways: 使用命名参数和可选参数会通过以下方式影响重载解析:

  • A method, indexer, or constructor is a candidate for execution if each of its parameters either is optional or corresponds, by name or by position, to a single argument in the calling statement, and that argument can be converted to the type of the parameter. 如果方法,索引器或构造函数的每个参数都是可选的,或者按名称或按位置对应于调用语句中的单个参数,并且该参数可以转换为参数的类型,则它是执行的候选对象。

  • If more than one candidate is found, overload resolution rules for preferred conversions are applied to the arguments that are explicitly specified. 如果找到多个候选者,则将首选转换的重载解析规则应用于显式指定的参数。 Omitted arguments for optional parameters are ignored. 忽略可选参数的忽略参数。

  • If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. 如果判断出两个候选者的性能相同,则优先选择不具有可选参数且在调用中省略了参数的候选者。 This is a consequence of a general preference in overload resolution for candidates that have fewer parameters. 这是对参数较少的候选对象在重载解析中普遍偏爱的结果。

So, no - you can't. 所以,不-您不能。

According to the C# language guide ( emphasis mine), 根据C#语言指南重点是我的),

Use of named and optional arguments affects overload resolution in the following ways: 使用命名参数和可选参数会通过以下方式影响重载解析:

... ...

If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call . 如果判断出两个候选者的性能相同,则优先选择不具有可选参数的候选者,该可选参数的调用中省略了参数 This is a consequence of a general preference in overload resolution for candidates that have fewer parameters. 这是对参数较少的候选对象在重载解析中普遍偏爱的结果。

You can use a third party analysis tool to flag this as an error or to use Visual Studio's built in static code analysis too with a custom rule that you implement. 您可以使用第三方分析工具将其标记为错误,也可以将Visual Studio的内置静态代码分析与实现的自定义规则一起使用。

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

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