When using object initializers, why does the compiler generate an extra local variable?

前端 未结 3 1735
难免孤独
难免孤独 2020-12-02 20:19

While answering a question on SO yesterday, I noticed that if an object is initialized using an Object Initializer, the compiler creates an extra local variable.

Con

3条回答
  •  借酒劲吻你
    2020-12-02 20:56

    For the Why: could be that it's done to ensure that no "known" reference to a not (fully) initialized object (from the language's point of view) exists? Something like (pseudo-)constructor semantics for the object initializer? But that's just an idea.. and I can't imagine a way to use the reference and access the not initialized object besides in a multi-threaded environment.

    EDIT: too slow..

提交回复
热议问题