1

Here is my code:

MyTool tool = new MyTool(new SubTool());
tool.PrintingSystem.Document.Target = 1;
tool.ShowPreview();

I want to know if it is possible to populate the PrintingSystem.Document.Target in initialization like this:

MyTool tool = new MyTool(new SubTool())
{
   PrintingSystem.Document.Target = 1
};

It does not work currently.

4
  • 2
    That doesn't work for you already? Commented Sep 5, 2018 at 8:29
  • 1
    Assuming that you already have the code in Visual Studio, why don't you press F5 and let us know. Commented Sep 5, 2018 at 8:29
  • 1
    Object and Collection Initializers (C# Programming Guide) Commented Sep 5, 2018 at 8:33
  • @TheGeneral: I don't think that guide covers this situation - where the OP is trying to set a property of a value that's created by default. I can't see any examples of it. Commented Sep 5, 2018 at 8:43

2 Answers 2

3

The equivalent of your original code with an object initializer is:

MyTool tool = new MyTool(new SubTool())
{
    PrintingSystem = { Document = { Target = 1 } }
};

This only calls the getters for PrintingSystem and then Document, then calls the setter for Target - just like your original code. This is a relatively rarely-used feature of object initializers, called nested object initializers. From the ECMA C# standard, section 12.7.11.3:

A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment (§12.18.2) to the field or property.

A member initializer that specifies an object initializer after the equals sign is a nested object initializer, i.e., an initialization of an embedded object. Instead of assigning a new value to the field or property, the assignments in the nested object initializer are treated as assignments to members of the field or property. Nested object initializers cannot be applied to properties with a value type, or to read-only fields with a value type.

It will only work if PrintingSystem and Document default to non-null values - otherwise you'd need to set the properties as per fubo's answer.

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

Comments

1

If a property is a object, refering to another object - you have to create a new instance of each to avoid a NullreferenceException

MyTool tool = new MyTool(new SubTool())
{
    PrintingSystem = new PrintingSystem() { Document = new Document() { Target = 1 } }
};

https://dotnetfiddle.net/1G39Zs

3 Comments

That's assuming the constructor doesn't already initialize it. If the OP's original code works, they don't need to do this - and may not be able to, e.g. if PrintingSystem is a read-only property.
right but OP didn't mention it in his question so we have to assume
We've clearly understood the question differently then. I read it as "here's code that does it in two statements, but works - how can I do it with an object initializer instead?" If the OP hadn't included their existing code, or had said it throws an exception, that would be a different matter.

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.