简体   繁体   中英

C# - auto-implemented events

In C# there are auto-implemented properties...

public int SomeValue { get; set; }

But why aren't there auto-implemented events?

public event EventHandler<SomeEventArgs> SomethingHappened { add; remove; }

Events are "auto-events" by default: when you write

public event EventHandler<SomeEventArgs> SomethingHappened;

you get the default behavior for both add and remove .

Note that C# designers could not use the same syntax for auto-properties because it would collide with field declarations. In addition, they needed to supply syntax for letting you define visibility of get and set separately, so they invented the { get ; set; } { get ; set; } { get ; set; } syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM