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

发布于 2024-11-02 01:19:35 字数 867 浏览 4 评论 0原文

我正在玩可选参数,看看它们如何与接口一起工作,我遇到了一个奇怪的警告。我的设置是以下代码:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生寂 2024-11-09 01:19:35

C# 中可选参数的问题在于被调用者是否将对象视为 TestClass 还是 ITestInterface。在第一种情况下,应用类中声明的值。在第二种情况下,应用接口中声明的值。这是因为编译器使用静态可用的类型信息来构造调用。在显式接口实现的情况下,永远不会“针对类”调用该方法,而始终“针对接口”

调用 C# 语言规范 10.6.1 中的规定:

如果可选参数出现在实现部分方法声明(第 10.2.7 节)、显式接口成员实现(第 13.4.1 节)或单参数索引器声明(第 10.9 节)中,编译器应发出警告,因为这些成员永远不能以允许省略参数的方式调用。

The problem with optional arguments in C# is whether the callee sees the object as a TestClass or an 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:

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.

伴梦长久 2024-11-09 01:19:35

编译器告诉您

void ITestInterface.TestOptional(int a = 5, int b = 5, object c = null)

相同

void ITestInterface.TestOptional(int a, int b, object c)

基本上与原因 ,因为您必须通过接口调用 TestOptional,接口将提供参数。在类中不可能没有为您提供参数值。

2020 年编辑:很快您就可以使用 C# 8 默认实现接口

interface ILogger
{
    void Log(LogLevel level, string message);
    void Log(Exception ex) => Log(LogLevel.Error, ex.ToString());
}

您可以轻松地执行类似以下操作:

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);

}

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. 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

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);

}
蓝色星空 2024-11-09 01:19:35

克雷格,

这些警告来自类方法实现中指定的默认值。在 .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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文