Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

c# - Delegates with same signature

My question is a bit similar to this one: How to convert an action to a defined delegate of the same signature?

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...