0

Whilst working in a codebase I found a line of code that seemed to initialize an CustomObject (ObservableObject to be precise) as follows:

CustomObject someObject = {Prop1="1", Prop2="2", Prop3="3"}

where CustomObject has three string properties Prop1, Prop2, Propr3. The problem is that I have not been able to reproduce such an initialization in a sample project of my own. What is this notation called? To me it seems as though list notation is used to initialize a custom object...how is this possible? It's certainly not an anonymous type that is being created.

Marcin
  • 131
  • 6
  • 2
    Exactly as written, that's just not legal C#. It would at the very least require a `new` (to make an anonymous type) or a `new()` (implicit type for constructor, with an initializer). If you omit the type (`someObject = { Prop1 = ... }`) it is legal as initialization syntax for a property or field, but only in the context of creating another object (i.e. `new ContainingObject { someObject = ... }`). Do you have the context in which this line occurred, or possibly a correction of what the *exact* line was? C# by now has acquired many such "initialization shorthands". – Jeroen Mostert Mar 03 '22 at 19:11
  • Yes, it's done as initialization syntax of a property, so my question should be: `var parentObjecct = new ParentObject(){ SomeObject = { Prop1="1", Prop2="2", Prop3="3"}};` But still, SomeObject is an object, how can we initialize it with list notation? – Marcin Mar 03 '22 at 19:24
  • 1
    That's not "list notation" (a collection initializer, which compiles to `Add` calls), that's a nested object initializer. It has been legal since C# 3 (so quite a while now). It's simply shorthand for `var o = new ParentObject(); o.SomeObject.Prop1 = "1"; o.SomeObject.Prop2 = "2"...` Note that this *requires* that `ParentObject` initialize `SomeObject` to an instance, otherwise it'll fail -- the sytax merely assigns, it doesn't create (in this it resembles collection initializers, which merely call `.Add` without creating). – Jeroen Mostert Mar 03 '22 at 19:36
  • Thanks @JeroenMostert. That answers it. But unfortunately I have no clue how to mark a comment as an answer. Cheers – Marcin Mar 03 '22 at 19:57

1 Answers1

-1

What you are seeing is a default property settings construction of the class.

By default, you may see

var SomeObject = new SomeObjectClass();
SomeObject.Prop1 = "1";
SomeObject.Prop2 = "2";
SomeObject.Prop3 = "3";

By using the curly brackets does the implied same result. You are just stating... give me an instance of a given object, and by the way, please set the following properties to these default values I'm providing via...

var SomeObject = new SomeObjectClass{ 
                         SomeObject.Prop1 = "1",
                         SomeObject.Prop2 = "2",
                         SomeObject.Prop3 = "3" };

Now, in the sample context you have provided there is no "new" of a given class trying to be created.

DRapp
  • 47,638
  • 12
  • 72
  • 142