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.

4
  • 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". Commented Mar 3, 2022 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? Commented Mar 3, 2022 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). Commented Mar 3, 2022 at 19:36
  • Thanks @JeroenMostert. That answers it. But unfortunately I have no clue how to mark a comment as an answer. Cheers Commented Mar 3, 2022 at 19:57

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.