在初始化列表中分配readonly属性

Spo*_*ook 3 c# properties readonly initializer-list

可以告诉我,为什么它会编译?

namespace ManagedConsoleSketchbook
{
    public interface IMyInterface
    {
        int IntfProp
        {
            get;
            set;
        }
    }

    public class MyClass
    {
        private IMyInterface field = null;

        public IMyInterface Property
        {
            get
            {
                return field;
            }
        }
    }

    public class Program
    {
        public static void Method(MyClass @class)
        {
            Console.WriteLine(@class.Property.IntfProp.ToString());
        }

        public static void Main(string[] args)
        {
            // ************
            // *** Here ***
            // ************

            // Assignment to read-only property? wth?

            Method(new MyClass { Property = { IntfProp = 5 }});
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 10

这是一个嵌套的对象初始值设定项.它在C#4规范中描述如下:

在等号后面指定对象初始值设定项的成员初始值设定项是嵌套对象初始值设定项 - 即嵌入对象的初始化.而不是为字段或属性分配新值,嵌套对象初始值设定项中的赋值被视为对字段或属性成员的赋值.嵌套对象初始值设定项不能应用于具有值类型的属性,也不能应用于具有值类型的只读字段.

所以这段代码:

MyClass foo = new MyClass { Property = { IntfProp = 5 }};
Run Code Online (Sandbox Code Playgroud)

相当于:

MyClass tmp = new MyClass();

// Call the *getter* of Property, but the *setter* of IntfProp
tmp.Property.IntfProp = 5;

MyClass foo = tmp;
Run Code Online (Sandbox Code Playgroud)