Why can't I initialize readonly variables in a initializer?

后端 未结 10 1938
渐次进展
渐次进展 2021-01-17 08:16

Why can\'t I initialize readonly variables in a initializer? The following doesn\'t work as it should:

class Foo
{
    public readonly int bar;
}

new Foo {          


        
10条回答
  •  无人共我
    2021-01-17 08:34

    Since readonly variables must be initialized in constructor, and property initializers execute after the construction of object, that is not valid.

提交回复
热议问题