[英]Warning From Explicitly Implementing an Interface with Optional Parameters
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:编译器给了我以下警告:
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.正如我所料,我得到了“ a=5 b=10 c=
”的输出。
My question is what is warning for?我的问题是警告是什么? What contexts is it referring to?它指的是什么语境?
The problem with optional arguments in C# is whether the callee sees the object as a TestClass
or an ITestInterface
. C# 中可选参数的问题是被调用者将对象视为TestClass
还是ITestInterface
。 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: 10.6.1 中的 C# 语言规范指出:
If optional parameters occur in an implementing partial method declaration (§10.2.7) , an explicit interface member implementation (§13.4.1) or in a single-parameter indexer declaration (§10.9) the compiler should give a warning, since these members can never be invoked in a way that permits arguments to be omitted.如果可选参数出现在实现部分方法声明(第 10.2.7 节)、显式接口成员实现(第 13.4.1 节)或单参数索引器声明(第 10.9 节)中,编译器应该发出警告,因为这些成员永远不能以允许省略参数的方式调用。
The compiler is telling you编译器告诉你
void ITestInterface.TestOptional(int a = 5, int b = 5, object c = null)
Is fundamentally the same as基本上是一样的
void ITestInterface.TestOptional(int a, int b, object c)
The reason being, since you have to invoke TestOptional through the interface the interface will supply the parameters.原因是,因为您必须通过接口调用 TestOptional,该接口将提供参数。 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 2020 年编辑:很快您将能够使用C# 8 默认实现接口来做到这一点
interface ILogger
{
void Log(LogLevel level, string message);
void Log(Exception ex) => Log(LogLevel.Error, ex.ToString());
}
You could just as easily do something akin to:您可以轻松地执行以下操作:
public interface ITestInterface
{
void TestOptional(int a, int b, object c);
void TestOptional() => TestOptional(5);
void TestOptional(int a) => TestOptional(a, 10)
void TestOptional(int a, int b) => TestOptional(a, b, null);
}
Eric Lippert在这里有一个非常明确的解释: 为什么接口上定义的C#4可选参数在实现类时没有强制执行?
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.在 .net 中,参数的默认值始终由引用类型决定。 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. Intellisense 会很好,因为这里的默认值永远不会有效。
http://funcakes.posterous.com/?tag=c http://funcakes.posterous.com/?tag=c
http://funcakes.posterous.com/c-40-optional-parameters-default-values-and-i http://funcakes.posterous.com/c-40-optional-parameters-default-values-and-i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.