简体   繁体   English

显式实现带有可选参数的接口的警告

[英]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:编译器给了我以下警告:

  • 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为参数“a”指定的默认值将无效,因为它适用于在不允许可选参数的上下文中使用的成员
  • 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为参数“b”指定的默认值将无效,因为它适用于在不允许可选参数的上下文中使用的成员
  • 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为参数“c”指定的默认值将无效,因为它适用于在不允许可选参数的上下文中使用的成员

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.

相关问题 在为WCF实现接口时,不能使用可选参数 - Can't use optional parameters when implementing an interface for a WCF 为什么在接口上定义的 C# 4 可选参数没有在实现类上强制执行? - Why are C# 4 optional parameters defined on interface not enforced on implementing class? 用抽象方法显式实现接口 - Explicitly implementing an interface with an abstract method 显式实现的接口中的可选参数 - Optional parameters in explicitly implemented interfaces 使用Visual Studio显式实现n接口 - Implementing n interface explicitly using Visual Studio 将泛型类型参数显式转换为任何接口 - explicitly cast generic type parameters to any interface SimpleInjector解决所有实现接口的类型,而无需显式绑定 - SimpleInjector resolving all types implementing an interface without explicitly binding 如何使用反射来获取显式实现接口的属性? - How do I use reflection to get properties explicitly implementing an interface? 编译错误:“显式实现接口时,修饰符'public'对此项无效” - Compilation Error: “The modifier 'public' is not valid for this item” while explicitly implementing the interface 显式地将派生类标记为基类的实现接口 - Explicitly marking derived class as implementing interface of base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM