显式实现带有可选参数的接口的警告
我正在玩可选参数,看看它们如何与接口一起工作,我遇到了一个奇怪的警告。我的设置是以下代码:
public interface ITestInterface
{
void TestOptional(int a = 5, int b = 10, object c = null);
}
public class TestClass : ITestInterface
{
void ITestInterface.TestOptional(int a = 5, int b = 5, object c = null)
{
Console.Write("a=" + a + " b=" + b + " c=" + c);
}
}
编译器给出以下警告:
- 为参数“a”指定的默认值将无效,因为它适用于在不允许可选参数的上下文中使用的成员
- 默认值为参数“b”指定的值将无效,因为它适用于在不允许可选参数的上下文中使用的成员。
- 为参数“c”指定的默认值将无效,因为它适用于在以下环境中使用的成员:不允许可选参数的上下文
如果我运行使用以下代码:
class Program
{
static void Main(string[] args)
{
ITestInterface test = new TestClass();
test.TestOptional();
Console.ReadLine();
}
}
我得到了“a=5 b=10 c=
”的输出,正如我所期望的那样。
我的问题是警告有什么用?它指的是哪些上下文?
I was playing with optional parameters to see how they would work with interfaces and I came across a strange warning. The setup I had was the following code:
public interface ITestInterface
{
void TestOptional(int a = 5, int b = 10, object c = null);
}
public class TestClass : ITestInterface
{
void ITestInterface.TestOptional(int a = 5, int b = 5, object c = null)
{
Console.Write("a=" + a + " b=" + b + " c=" + c);
}
}
The compiler gives me the following warnings:
- The default value specified for parameter 'a' will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
- The default value specified for parameter 'b' will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
- The default value specified for parameter 'c' will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
If I run this with the following code:
class Program
{
static void Main(string[] args)
{
ITestInterface test = new TestClass();
test.TestOptional();
Console.ReadLine();
}
}
I get the output of "a=5 b=10 c=
" as I'd expect.
My question is what is warning for? What contexts is it referring to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C# 中可选参数的问题在于被调用者是否将对象视为
TestClass
还是ITestInterface
。在第一种情况下,应用类中声明的值。在第二种情况下,应用接口中声明的值。这是因为编译器使用静态可用的类型信息来构造调用。在显式接口实现的情况下,永远不会“针对类”调用该方法,而始终“针对接口”调用 C# 语言规范 10.6.1 中的规定:
The problem with optional arguments in C# is whether the callee sees the object as a
TestClass
or anITestInterface
. In the first case, the values declared in the class apply. In the second case the values declared in the interface apply. It is because the compiler uses the statically available type information to construct the call. In case of an explicit interface implementation the method is never called 'for a class', always 'for an interface'The C# Language Specification in 10.6.1 states:
编译器告诉您
相同
基本上与原因 ,因为您必须通过接口调用 TestOptional,接口将提供参数。在类中不可能没有为您提供参数值。
2020 年编辑:很快您就可以使用 C# 8 默认实现接口
您可以轻松地执行类似以下操作:
The compiler is telling you
Is fundamentally the same as
The reason being, since you have to invoke TestOptional through the interface the interface will supply the parameters. There is no way at the class for you to have not been supplied a parameter value.
2020 edit: soon you will be able to do this with C# 8 by default implementations of interfaces
You could just as easily do something akin to:
克雷格,
这些警告来自类方法实现中指定的默认值。在 .net 中,参数的默认值始终由引用类型确定。当然,使用这样的显式接口实现,只能通过接口引用调用此方法 - 它定义了默认值。因此,你在课堂上放什么值当然无关紧要,因为它永远不会被解决,你可以愉快地删除它。智能感知会很好,因为这里的默认值永远不会有效。
http://funcakes.posterous.com/?tag=c
http://funcakes.posterous.com/c-40-可选参数-default -值-和-i
Craig,
These warnings are coming from the default values specified in class method implementation. In .net, the default value for the argument is always determined by the reference type. And of course, with an explicit interface implementation like this it is only possible to call this method via an interface reference - which defines the default value. As such it's of course irrelevant what value you put here in the class, since it'll never ever be resolved and you can remove it happily. Intellisense will be fine, given that the default value here can never ever be effective.
http://funcakes.posterous.com/?tag=c
http://funcakes.posterous.com/c-40-optional-parameters-default-values-and-i