Assigning events in object initializer

前端 未结 3 1177
渐次进展
渐次进展 2020-11-29 11:11

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()
         


        
3条回答
  •  一生所求
    2020-11-29 11:36

    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:

    var timer = new DispatcherTimer {
        Tick += delegate {},
        Interval = TimeSpan.FromSeconds(1d),
    };
    

提交回复
热议问题