0

If I use an object initializer when instantiating an object does that objects constructor have access to the initialized properties?

public class ExampleClass {
    public string proptery1 { get; set; }
    public string property2 { get; set; }

    public ExampleClass() {
        if(!string.IsNullOrEmpty(property1)) {
            ...
        }
    }
} 

ExampleClass exampleClass = new ExampleClass() {
    property1 = "hello",
    property2 = "world"
};
bflemi3
  • 6,698
  • 20
  • 88
  • 155
  • 3
    The syntax you use is not a collection initializer. It is object initializer. – Grigory Aug 01 '12 at 15:53
  • I asked the question because maybe there's a different way to do what I'm trying to do. I don't know what I don't know. Thanks for the down votes... – bflemi3 Aug 01 '12 at 16:07

3 Answers3

1

Collection initializers call the .Add method of the collection, which must be defined for the particular collection. The object will be fully constructed before it is passed to .Add.

The syntax

ExampleClass exampleClass = new ExampleClass() {
    property1 = "hello",
    property2 = "world"
};

does not show a collection initializer, but rather object initialization.

Here, your constructor will be called, then setters for the given properties will be called.

A proper example would be

List<ExampleClass> list = new List<ExampleClass>() { 
    new ExampleClass() {
         exampleClass.property1 = "hello";
         exampleClass.property2 = "world";            
    }
}

The order of events would be

  1. A new instance of List<ExampleClass> is created and assigned to list
  2. A new instance of ExampleClass is created, the constructor called.
  3. The property setters are called on the new instance of ExampleClass
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 2
    Why the downvote? If something is wrong, please do share what. – Eric J. Aug 01 '12 at 15:59
  • 1
    DV only for the wrong answer. You first wrote wrong answer (that c-tor is called "after backing fields are initialized". There are not backing fields in question. And for properties ctor is called BEFORE. Did you answer just to be first and to get scores ? I think we all should care about correctness. And of course if you're not sure - just don't answer. Nobody forces you to giving wrong answer. Of course now you've fixed answer. But it was so displeasingly to see the wrong answer that was fixed after probably debugging the code in IDE. Programming is not speed. It is more correctness. – Grigory Aug 01 '12 at 16:06
  • 1
    @Grigory: I misread the question at first, which is why I deleted my first answer. Downvoting the answer later after it has been corrected, and assuming ill intent with no further evidence, is not appropriate. – Eric J. Aug 01 '12 at 16:12
1

No, the constructor is called before any properties are initialized.

Your code:

ExampleClass exampleClass = new ExampleClass() {
    property1 = "hello",
    property2 = "world"
};

is language-sugar for

ExampleClass exampleClass = new ExampleClass();
exampleClass.property1 = "hello";
exampleClass.property2 = "world";
Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

No, the constructor gets called first. Object initialization is only a syntactic sugar for calling the constructor and then settings object's properties.

dzendras
  • 4,721
  • 1
  • 25
  • 20