在对象初始值设定项中分配事件
为什么不能在 C# 中的对象初始值设定项中将事件与属性一起分配?这样做似乎是那么自然。
var myObject = new MyClass()
{
Property = value,
Event1 = actor,
// or
Event2 += actor
};
还是有什么我不知道的技巧?
Why isn't it possible to assign events along with properties in object initializers in C#? It seems to be so natural to do so.
var myObject = new MyClass()
{
Property = value,
Event1 = actor,
// or
Event2 += actor
};
Or is there some trick that I don't know of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并没有使 C# 6 或 C# 7 (自最初的问题以来),但尚未决定反对。 GitHub 上有一个跟踪该语言提案的问题。您可以在那里投票,也可以点击链接进入之前围绕该功能的讨论。
https://github.com/dotnet/csharplang/issues/307
如果您如果您想看到此功能,请为该问题点赞,以帮助提高其可见度。
建议的语法是:
This didn't make C# 6 or C# 7 (since the original question), however it hasn't been decided against. There is an issue on GitHub that tracks the language proposal. You can vote for it there, as well as follow links into previous discussion around the feature.
https://github.com/dotnet/csharplang/issues/307
If you'd like to see this feature, add a thumbs-up to the issue to help raise its visibility.
The proposed syntax is:
就外部合约而言,事件没有 setter,只有
add
和remove
方法 - 订阅者可以注册和取消注册事件,发布对象通过“引发”事件来决定何时调用回调。因此,“分配事件”的想法一般来说是没有意义的。然而,当您在类中声明事件时,C# 编译器会为您提供真正便利的功能:当您不提供自己的实现时,它会创建一个私有、支持委托。字段,以及适当的添加/删除实现。这允许您在类内“设置事件”(实际上是支持字段),但不能在类外部“设置事件”。要理解这一点,请考虑:
As far the external contract is concerned, an event doesn't have a setter, only
add
andremove
methods - subscribers can register and unregister from the event, and the publishing object decides when to invoke the callbacks by 'raising' the event. Consequently, the idea of "assigning an event", in general, is meaningless.However, when you declare an event in a class, the C# compiler provides you with what is really a convenience-feature: when you don't provide your own implementation, it creates a private, backing delegate-field for you, along with the appropriate add / remove implementations . This allows you to "set the event" (really the backing field) within the class, but not outside it. To understand this, consider:
您只能将运算符
+=
或-=
用于其所有者类之外的事件。您无法删除类中定义的
OnSave
操作。您只能在类外部添加/删除自己的OnSave
操作。如果删除event
关键字,OnSave
将不再是一个事件,而是一个普通的委托。然后你可以做任何事情,包括在类外赋值。You can only use the operators
+=
or-=
to an event outside its owner class.You can't remove the
OnSave
action defined in the class. You can only add/remove your ownOnSave
actions outside the class. If you remove theevent
keyword, theOnSave
will be no longer an event, but an ordinary delegate. Then you can do anything including assigning value outside the class.