dotnet / dotnet/csharpstandard

C#6 §11.11.9 (Delegate equality operators) vs. delegate variance

Open
#663 8 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

ECMA-334:2022 §11.11.9 (Delegate equality operators) specifies:

> If operator overload resolution resolves to either delegate equality operator, and the binding-time types of both operands are delegate types as described in §19 rather than `System.Delegate`, and there is no identity conversion between the binding-type operand types, a binding-time error occurs.
>
> _Note:_ This rule prevents comparisons which can never consider non-`null` values as equal due to being references to instances of different types of delegates. _end note_

Does that require a binding-time error in this case:

```C#
namespace DelegateVariance
{
delegate void Dee(T value);

class Program
{
static void Main()
{
Dee d1 = Ignore;
Dee d2 = d1;

System.Console.WriteLine(d1 == d2);
}

static void Ignore(object obj) {}
}
}
```

If I understand correctly, the binding-time types are Dee\ and Dee\, and there is no identity conversion between those, although there is an implicit reference conversion from Dee\ to Dee\. The standard would then seem to require a binding-time error, but:

- The rationale in the note does not apply because the comparison returns true if it is allowed to execute.
- Roslyn 4.3.0-3.22465.2+7d8de40e2bfe5bd2546ca49177166741af4c8e07 does not report a compile-time error for this.

(Inspired by .)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.