How can I use the curly braces from object constructor when modifying an existing instance?
I tried shallow and deep-copying in the constructor, but there doesn't seem to be a way to swap out this
with the desired object instance, or a simple and reliable way to set all of the (ever-changing during development) fields and properties inside the constructor.
As you can see here, curly braces save a lot of space compared to the old get { return ... }
way:
public static MyClass MyClass1 => new MyClass()
{
Property1 = 20,
Property2 = "some text",
Property3 = new MyOtherClass(MyEnum.Something)
};
Basically the same code but with extra 5 lines:
public static MyClass MyClass2
{
get
{
var instance = new MyClass(); // or it could be = MyClass1;
instance.Property1 = 42;
instance.Property2 = "some other text";
instance.Property3 = new MyOtherClass(MyEnum.SomethingElse);
return instance;
}
}
My goal is to use the curly braces to set new property values of a base object (without inheritance or manually doing a shallow/deep copy in the constructor) to save all that vertical space. How can I do that?
I'm using full document auto-formatting often, so manually formatting the code and just not touching it is not going to work.
The issue I am trying to solve: get { ... }
-style getter is taking up 5 extra lines of code when auto-formatted. =>
- style getter is much more compact, and I want to find a way to use it instead.