为什么在接口上定义的 C# 4 可选参数没有在实现类上强制执行?
我注意到,对于 C# 4 中的可选参数,如果您在接口上指定可选参数,则不必必须在任何实现类上将该参数设置为可选:
public interface MyInterface
{
void TestMethod(bool flag = false);
}
public class MyClass : MyInterface
{
public void TestMethod(bool flag)
{
Console.WriteLine(flag);
}
}
因此:
var obj = new MyClass();
obj.TestMethod(); // compiler error
var obj2 = new MyClass() as MyInterface;
obj2.TestMethod(); // prints false
有谁知道为什么可选参数被设计成这样工作?
一方面,我认为覆盖接口上指定的任何默认值的能力很有用,但老实说,我不确定您是否应该能够在接口上指定默认值,因为这应该是一个实现决策。
另一方面,这种脱节意味着您不能总是互换使用具体类和接口。当然,如果在实现上指定了默认值,这不会成为问题,但是如果您将具体类公开为接口(例如使用某些 IOC 框架来注入具体类),那么实际上就没有问题了点具有默认值,因为调用者无论如何都必须始终提供它。
I noticed that with the optional parameters in C# 4 if you specify an optional parameter on an interface you don't have to make that parameter optional on any implementing class:
public interface MyInterface
{
void TestMethod(bool flag = false);
}
public class MyClass : MyInterface
{
public void TestMethod(bool flag)
{
Console.WriteLine(flag);
}
}
and therefore:
var obj = new MyClass();
obj.TestMethod(); // compiler error
var obj2 = new MyClass() as MyInterface;
obj2.TestMethod(); // prints false
Does anyone know why optional parameters are designed to work this way?
On one hand I suppose the ability to override any default values specified on the interfaces is useful though to be honest I'm not sure if you should even be able to specify default values on the interface as that should be an implementation decision.
On the other hand, this disconnect means you can't always use the concrete class and the interface interchangeably. This of course, wouldn't be a problem if the default value is specified on the implementation, but then if you're exposing your concrete class as the interface (using some IOC framework to inject the concrete class for instance) then really there's no point having the default value as the caller will have to always provide it anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
更新:这个问题是我五月份博客的主题2011 年 12 日。感谢提出这个好问题!
假设您有一个如您所描述的接口,以及一百个实现它的类。然后,您决定将接口方法之一的参数之一设置为可选。您是否建议正确的做法是编译器强制开发人员找到该接口方法的每个实现,并使参数也可选?
假设我们这样做了。现在假设开发人员没有实现的源代码:
D 的作者应该如何使其工作?在你们的世界里,他们是否需要打电话给 B 的作者,并要求他们给他们发送一个新版本的 B,使该方法具有可选参数?
那是不会飞的。如果两个人给 B 的作者打电话,其中一人希望默认值为 true,而其中一人希望默认值为 false,该怎么办?如果 B 的作者根本不配合怎么办?
也许在这种情况下,他们需要说:
所提议的功能似乎给程序员带来了很多不便,而代表权力却没有相应增加。该功能有什么引人注目的好处来证明用户增加的成本是合理的?
更新:在下面的评论中,supercat 建议了一种语言功能,该功能将真正增强该语言的功能,并启用一些类似于本问题中描述的场景。仅供参考,该功能(接口中方法的默认实现)将添加到 C# 8 中。
UPDATE: This question was the subject of my blog on May 12th 2011. Thanks for the great question!
Suppose you have an interface as you describe, and a hundred classes that implement it. Then you decide to make one of the parameters of one of the interface's methods optional. Are you suggesting that the right thing to do is for the compiler to force the developer to find every implementation of that interface method, and make the parameter optional as well?
Suppose we did that. Now suppose the developer did not have the source code for the implementation:
How is the author of D supposed to make this work? Are they required in your world to call up the author of B on the phone and ask them to please ship them a new version of B that makes the method have an optional parameter?
That's not going to fly. What if two people call up the author of B, and one of them wants the default to be true and one of them wants it to be false? What if the author of B simply refuses to play along?
Perhaps in that case they would be required to say:
The proposed feature seems to add a lot of inconvenience for the programmer with no corresponding increase in representative power. What's the compelling benefit of this feature which justifies the increased cost to the user?
UPDATE: In the comments below, supercat suggests a language feature that would genuinely add power to the language and enable some scenarios similar to the one described in this question. FYI, that feature -- default implementations of methods in interfaces -- will be added to C# 8.
可选参数只是用属性标记。此属性告诉编译器在调用站点插入该参数的默认值。
当 C# 代码编译为 IL 时(而不是在 JIT 时),调用
obj2.TestMethod();
被替换为obj2.TestMethod(false);
。因此,在某种程度上,调用者总是提供带有可选参数的默认值。这也会对二进制版本控制产生影响:如果更改默认值但不重新编译调用代码,它将继续使用旧的默认值。
如果接口方法是显式实现,那么您就无法执行此操作。
An optional parameter is just tagged with an attribute. This attribute tells the compiler to insert the default value for that parameter at the call-site.
The call
obj2.TestMethod();
is replaced byobj2.TestMethod(false);
when the C# code gets compiled to IL, and not at JIT-time.So in a way it's always the caller providing the default value with optional parameters. This also has consequences on binary versioning: If you change the default value but don't recompile the calling code it will continue to use the old default value.
You already can't do that if the interface method was implemented explicitly.
因为默认参数是在编译时解析的,而不是运行时解析的。
因此,默认值不属于被调用的对象,而是属于被调用的引用类型。
Because default parameters are resolved at compile time, not runtime.
So the default values does not belong to the object being called, but to the reference type that it is being called through.
据我了解,可选参数有点像宏替换。从方法的角度来看,它们并不是真正可选的。其中一个工件是您看到的行为,如果您转换到接口,您会得到不同的结果。
Optional parameters are kind of like a macro substitution from what I understand. They are not really optional from the method's point of view. An artifact of that is the behavior you see where you get different results if you cast to an interface.
只是想在这里添加我的看法,因为其他答案确实提供了合理的解释,但并不是完全令我满意的。
可选参数是用于在调用站点编译时注入默认值的语法糖。这与接口/实现没有任何关系,它可以被视为纯粹具有可选参数的方法的副作用。因此,当您调用该方法(
如
SomeClass.TestMethod()
)时,它实际上是SomeClass.TestMethod(false)
。如果您在接口上调用此方法,则通过静态类型检查,方法签名具有可选参数。如果在不具有可选参数的派生类实例上调用此方法,则通过静态类型检查,方法签名不具有可选参数,并且必须使用完整参数进行调用。由于可选参数的实现方式,这是自然的设计结果。
Just want to add my take here, as the other answers do provide reasonable explanations, but not ones that fully satisfy me.
Optional parameters are syntactic sugar for compile-time injection of the default value at the call site. This doesn't have anything to do with interfaces/implementations, and it can be seen as purely a side-effect of methods with optional parameters. So, when you call the method,
like
SomeClass.TestMethod()
, it is actuallySomeClass.TestMethod(false)
. If you call this method on an interface, from static type-checking, the method signature has the optional parameter. If you call this method on a deriving class's instance that doesn't have the optional parameter, from static type-checking, the method signature does not have the optional parameter, and must be called with full arguments.Due to how optional parameters are implemented, this is the natural design result.
使用
和 都会导致 false
Use
and both result in false
感谢您的解释@eric-lippert
这是一些代码示例:
Thanks for your explanation @eric-lippert
Here is some code example: