简体   繁体   中英

In C#, Do Properties of a Class Get Initialized Concurrently Or Sequentially?

Suppose I'm composing an object whose properties need to be converted from other values determined in a method, such as:

var var1;
var var2;

//do stuff with var1 and var2

MyClass myClass = new MyClass
{
    Property1 = var1,
    Property2 = var2
};

If the properties of MyClass are both evaluated and set at the same time (where this is possible), I think it may be more efficient to devolve the operations that set var1 and var2 to separate methods, like this:

MyClass myClass = new MyClass
{
    Property1 = SetProperty1(),
    Property2 = SetProperty2()
}

private static object SetProperty1()
{
    //do stuff
}

private static object SetProperty2()
{
    //do stuff
}

If, however, each property is evaluated in order (as one sees when stepping through code like this), any such benefit from breaking out new methods is lost.

Do properties of a new ed object get initialized concurrently, or in order?

It is important to understand what happens here.

This statement:

MyClass myClass = new MyClass
{
    Property1 = var1,
    Property2 = var2
};

Actually compiles down to this:

MyClass temp = new MyClass();
temp.Property1 = var1;
temp.Property2 = var2;
MyClass myClass = temp;

The reason for this temp variable is that if you're instead of assigning to a local variable assign it to a property, that property won't contain a halfway-initialized object. It will only receive the object instance once initialization is complete.

So to answer your question, object initializers will assign the property values sequentially, and also in the order you wrote them, not in the order the corresponding properties are defined in the type, if this differ. This is because it might be important to order the side-effects assigning to properties might have, though I would advice against creating objects with such side-effects as they can be quite brittle.

However , any code inspecting the myClass variable (or field, or property) will either see the old value (likely null ), or the completely (according to your assignments) assigned object.


As an example of what would happen if you didn't use this temp variable:

MyClass myClass = new MyClass();
// what if some other code can access myClass here, because you're really
// assigning to a property or to a field?
myClass.Property1 = var1;
// then this assignment above wouldn't be done yet
...

They are set sequentially.

See http://csharp.net-tutorials.com/csharp-3.0/object-initializers/ for some examples of what it does 'under the hood'.

Just like the others said: the properties are initialized in the order you specify the assignments.

You can try this:

class MyClass
{
    private object _property1;
    private object _property2;

    public object Property1 
    { 
        get
        {
            return _property1; 
        }

        set
        {
            Console.WriteLine("Property1 set");
            _property1 = value;
        }
    }

    public object Property2 
    {
        get
        {
            return _property2;
        }

        set
        {
            Console.WriteLine("Property2 set");
            _property2 = value;
        }
    }
}

This is approximately what is generated, when you use the public object Property { get; set; } public object Property { get; set; } public object Property { get; set; } syntax. Except for the Console.WriteLine

When you use this class like this:

MyClass myClass = new MyClass
{
     Property2 = 1,
     Property1 = "a"
};

The output will be:

Property2 set
Property1 set

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM