简体   繁体   English

C# - 自动实现的事件

[英]C# - auto-implemented events

In C# there are auto-implemented properties... 在C#中有自动实现的属性......

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 . 您将获得addremove的默认行为。

Note that C# designers could not use the same syntax for auto-properties because it would collide with field declarations. 请注意,C#设计者无法对自动属性使用相同的语法,因为它会与字段声明冲突。 In addition, they needed to supply syntax for letting you define visibility of get and set separately, so they invented the { get ; set; } 另外,他们需要提供语法让你分别定义getset可见性,所以他们发明了{ get ; set; } { get ; set; } { get ; set; } syntax. { get ; set; }语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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