In C# all delegate types are incompatible with one another, even if they have the same signature. As an example:
delegate void D1();
delegate void D2();
D1 d1
In C# all delegate types are incompatible with one another, even if they have the same signature. What is the reasoning behind this behaviour and language design decision?
First off, I think that it is fair to say that many of the runtime and language designers regret this decision. Structural typing on delegates -- that is, matching by signature -- is a frequently requested feature and it just seems strange that Func
and Predicate
can't be freely assigned to each other.
The reasoning behind the decision as I understand it -- and I hasten to add that this decision was made about six years before I started on the C# team -- is that the expectation was that there would be delegate types with semantics. You want this to be a type error:
AnyFunction af = x=> { Console.WriteLine(x); return x + y; };
PureFunction pf = af;
A "pure" function is a function which produces and consumes no side effects, consumes no information outside of its arguments, and returns a consistent value when given the same arguments. Clearly af
fails at least two of those, and so should not be assignable to pf
as an implicit conversion.
But semantics-laden delegate types never happened, so it's a bit of a misfeature now.