Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Language Design: explicit interface implementation of an event

Small question about C# language design :))

If I had an interface like this:

interface IFoo {   int Value { get; set; } } 

It's possible to explicitly implement such interface using C# 3.0 auto-implemented properties:

sealed class Foo : IFoo {   int IFoo.Value { get; set; } } 

But if I had an event in the interface:

interface IFoo {   event EventHandler Event; } 

And trying to explicitly implement it using field-like event:

sealed class Foo : IFoo {   event EventHandler IFoo.Event; } 

I will get the following compiler error:

error CS0071: An explicit interface implementation of an event must use event accessor syntax

I think that field-like events is the some kind of dualism for auto-implemented properties.

So my question is: what is the design reason for such restriction done?

like image 537
controlflow Avatar asked Feb 15 '10 18:02

controlflow


1 Answers

Interesting question. I did some poking around the language notes archive and I discovered that this decision was made on the 13th of October, 1999, but the notes do not give a justification for the decision.

Off the top of my head I don't see any theoretical or practical reason why we could not have field-like explicitly implemented events. Nor do I see any reason why we particularly need to. This may have to remain one of the mysteries of the unknown.

like image 67
Eric Lippert Avatar answered Oct 09 '22 00:10

Eric Lippert