在初始化列表中给只读属性赋值

3

有人能告诉我,它为什么能编译通过吗?

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 }});
        }
    }
}
3个回答

13

这是一个嵌套对象初始化器。它在C# 4规范中被描述为:

在等号后指定对象初始化器的成员初始化器是一个嵌套对象初始化器 - 即嵌入对象的初始化。与将新值分配给字段或属性不同,在嵌套对象初始化器中的赋值被视为对字段或属性成员的赋值。不能将嵌套对象初始化器应用于具有值类型的属性,或使用值类型的只读字段。

所以这段代码:

MyClass foo = new MyClass { Property = { IntfProp = 5 }};

等同于:

MyClass tmp = new MyClass();

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

MyClass foo = tmp;

好的,现在清楚了。但这是一个令人困惑的语法片段。 - Spook

1
因为您使用的是使用 ItfProp 的 setter 的初始化器,而不是 Property 的 setter。因此,在运行时它会抛出一个 NullReferenceException,因为 Property 仍然是 null

你能详细说明一下吗?标准如何涵盖这种情况? - Spook
稍等,正在启动Visual Studio,发布一些澄清的例子。 - bas
2
啊,Jon在这里,我无法与他竞争 :p - bas
@Spook,不要误解,“Property”仍将为“null”。 - bas

0

因为

int IntfProp {
    get;
    set;
}

不是只读的。

你没有调用MyClass.Property 的 setter,只调用了 getter。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接