Spook
Spook

Reputation: 25927

Assignment to readonly property in initializer list

Can one tell me, why the heck does it compile?

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 }});
        }
    }
}

Upvotes: 3

Views: 1022

Answers (3)

bas
bas

Reputation: 14912

Because you are using the initializer which uses the setter of ItfProp, not the setter of Property.

So it will throw a NullReferenceException at runtime, since Property will still be null.

Upvotes: 1

Ken Kin
Ken Kin

Reputation: 4693

Because

int IntfProp {
    get;
    set;
}

is not readonly.

You did not invoke setter of MyClass.Property, just getter.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500913

This is a nested object initializer. It's described in the C# 4 spec like this:

A member initializer that specifies an object initializer after the equals sign is a nested object initializer - that is, an initialization of an embedded object. Instead of assigning a new value to the field or property, the assignments in the nested object initializer are treated as assignments to members of the field or property. Nested object initializers cannot be applied to properties with a value type, or to read-only fields with a value type.

So this code:

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

would be equivalent to:

MyClass tmp = new MyClass();

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

MyClass foo = tmp;

Upvotes: 13

Related Questions