1

In c#, does an object initialization like:

var thing = new List<object>() {new object()};

Occur before the assignment, so that it's approximately the same as:

var _thing = new List<object>();
_thing.Add(new object());
var thing = _thing;

Or does it occur after the assignment, so that it's approximately the same as:

var thing = new List<object>();
thing.Add(new object());

This would make a difference if you are trying to tie a recursive knot in a factory like:

static class AbstractFactory {
    private static readonly IEnumerable<object> _list = new List<object>() {GetIEnumerable()};

    public static IEnumerable<object> GetIEnumerable() {
        return _list;
    }
}

When _list is returned from GetIEnumerable when the method is called in the initializer for _list, will it be the new List<object>, null, or undefined?

Cirdec
  • 24,019
  • 2
  • 50
  • 100
  • 5
    Why don't you just execute your own code snippet there and find out for yourself? – Servy Sep 20 '13 at 16:49
  • 1
    Note your usage sample is very strange - assuming it only doing as an example... Otherwise it is very unclear what one should expect as result from that code. Maybe you mean `_list = new List(Enumerable.Empty())`? – Alexei Levenkov Sep 20 '13 at 16:59
  • @AlexeiLevenkov Clearly it's a contrived example that's simply designed to be some code sample that would act differently based on the exact semantics of this operation. In the vast majority of cases it just wouldn't matter. Finding a non-contrived example where it matter is likely possible, but a lot harder. There's nothing wrong with it here. – Servy Sep 20 '13 at 17:00
  • @AlexeiLevenkov `List<>` is just an object we are all familiar with. Will `GetIEnumerable()` be a `List<>` that contains itself or a `List<>` that contains `null`? – Cirdec Sep 20 '13 at 17:01
  • @Cirdec I'm inclined to answer, but given that you've done 95% of the work already, I'm surprised that you haven't just run your own complete code example and seen for yourself. There are two sensible results, and depending on which you see you can determine which behavior is used, or at least have some pretty darn strong indications. – Servy Sep 20 '13 at 17:04
  • @Servy If I execute it myself, it comes back as not null. I don't know if it's due to the meaning of the object initialization syntax, or due to some subtlety in the definition of static variables or how uninitialized variables are referenced. – Cirdec Sep 20 '13 at 17:09
  • @Cirdec That should be included in the question then. Show how this example gives string evidence in support of one of the cases, but you're curious if it is because of the reason you think it is, or if it's a result of a flawed experiment. – Servy Sep 20 '13 at 17:11

1 Answers1

2

It compiles down to first example i.e. object initialization occurs before assignment.

You can look at the compiled version using .Net Reflector. It compiled down to this (extracted using reflector) -

List<object> <>g__initLocal0 = new List<object>();
<>g__initLocal0.Add(new object());
List<object> thing = <>g__initLocal0;
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185