加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#4.0默认参数

发布时间:2020-12-16 01:49:05 所属栏目:百科 来源:网络整理
导读:考虑以下控制台应用程序: class Program{ static void Main() { MyInterface test = new MyClass(); test.MyMethod(); Console.ReadKey(); }}interface MyInterface{ void MyMethod(string myString = "I am the default value on the interface");}class M
考虑以下控制台应用程序:

class Program
{
    static void Main()
    {
        MyInterface test = new MyClass();
        test.MyMethod();

        Console.ReadKey();
    }
}

interface MyInterface
{
    void MyMethod(string myString = "I am the default value on the interface");
}

class MyClass : MyInterface
{
    public void MyMethod(string myString = "I am the default value set on the implementing class")
    {
        Console.WriteLine(myString);
    }
}

该程序的输出是:

I am the default value on the interface

(1)为什么没有在没有提供值的情况下在接口上指定参数为可选的方法.我认为默认值是实现细节.如果我们以pre-optional参数样式编写此代码,我们将在接口中创建两个重载,并在实现类中指定默认值.即我们会有:

interface MyInterface
{
    void MyMethod();

    void MyMethod(string myString);
}

class MyClass : MyInterface
{
    public void MyMethod()
    {
        MyMethod("I am the default value set on the implementing class");
    }

    public void MyMethod(string myString)
    {
        Console.WriteLine(myString);
    }
}

哪个输出正如我们所料,

I am the default value set on the implementing class

(2)为什么我们不能覆盖实现类中的默认值!

解决方法

.Net中的默认值实际上是基于编译器的语法糖.在调用站点,编译器会为您添加默认值.它无法在编译时知道对象的运行时类型,因此必须插入接口中定义的值.

因此,它们不能在实现中被“覆盖”,因为没有什么可以覆盖.

Eric Lippert撰写了一系列关于可选参数主题的非常有趣的博客文章,其中第一篇可以找到here.

更新
根据您的评论,您建议的是某种形式的“虚拟”参数(运行时类型声明),CLR必须“知道”.我猜这个实现被排除了,因为成本(设计,记录,实施,测试等)与它给出的好处相比太高了(虽然这只是猜测!).
或者,有默认的委托方法选项,即:

void M(bool y = false) { ... whatever ... }

由编译器重写为:

void M() { M(false); }
void M(bool y) { ... whatever ... }

但是,一旦考虑了多个可选参数和命名参数,那么沿着这条路线导致潜在的不可接受的重载级别.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读