具有相同签名的代表 [英] Delegates with same signature

查看:117
本文介绍了具有相同签名的代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与此类似:为什么具有相同签名的委托人之间没有隐式转换.例如,代码:

Why there is no implicit convertion between delegates with same signature. For example, code:

class Program
{
    private delegate void Foo1(int x);
    private delegate void Foo2(int x);


    static void Main(string[] args)
    {
        Foo1 foo1 = Console.WriteLine;
        Foo2 foo2 = Console.WriteLine;

        Call(foo1);

        Call2(foo2);
    }

    static void Call(Action<int> action)
    {
        action(10);
    }

    static void Call2(Foo1 action)
    {
        action(10);
    }
}

它不能编译,因为there isn't implicit convertion from Action<int> to Foo1. 但是通常这是同一回事.因此,这意味着该名称是别名,而不是实际名称.因此,我认为将其像别名一样考虑是一个好主意.因此,在这种情况下,我们有3个delegate, that get one int value and returns nothing的别名.而且这些代表可以彼此完全互换.但是我们没有.所以问题是:为什么?通过签名是同一回事,并且没有任何实现,因此具有相同签名的委托是相同的,并且具有许多别名...

it does not compile because there isn't implicit convertion from Action<int> to Foo1. But normaly it's the same thing. So it mean this names are aliases, not actualy names. So i think it was great idea to think about it like aliases. So in this case we have 3 aliases of a delegate, that get one int value and returns nothing. And this delegates are fully interchangeable one by another. But we don't have it. So question is: why? By signatures it's the same thing, and there isn't any implementation, so delegates with same signature are one and same with many aliases...

是C#缺陷还是有原因?对于我来说,我什么都看不到.

Is it C# defect or there are reasons for it? As to me, i don't see any.

推荐答案

这两个委托之间没有隐式转换,原因是这两种类型之间没有隐式转换:

There's no implicit conversion between those two delegates for the same reason that there's no implicit conversion between these two types:

public sealed class Foo1
{
    public string Value { get; set; }
}

public sealed class Foo2
{
    public string Value { get; set; }
}

仅仅因为两个类具有相同的字段并不意味着您应该能够将一个类视为另一个.相同的逻辑适用于委托(请注意,它们也是类型).

Just because two classes have the same fields doesn't mean that you should be able to treat one as if it were another. The same logic applies to delegates (which are also types, mind you).

创建该类型具有语义上的意义.如果有人创建了Foo1,他们希望它是Foo1,而不是Foo2.如果他们不愿使用期望使用Foo2Foo1,那是一个很大的危险信号,即使类型看起来相似,这两种类型在语义上也有所不同.如果程序员知道编译器不知道的内容,则可以使用某种形式的显式转换来表明他们知道自己在做什么.

There is semantic meaning applied to the creation of that type. If someone created a Foo1 they want it to be a Foo1, not a Foo2. If they're going out of their way to use a Foo1 where a Foo2 is expected, it's a big red flag that even though the types appear similar, there is a semantic difference between these two types. If the programmer knows something that the compiler doesn't, they can use an explicit conversion of some sort to indicate that they know what they're doing.

(上一段故意写成同样适用于您的代表以及我上面提供的类.)

(The previous paragraph was intentionally written to apply equally to your delegates, and the classes I provided above.)

这篇关于具有相同签名的代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆