Let's say I have a class with many members like this
class someclass
{
public int number {get;set;} //repeated a dozen times, with different names of course
}
it's constructor should look like this
public someclass(int number, ...,)
{
this.number=number; //repeated again a dozen times
}
is there any shorthand notation to avoid the repetition in the constructor?
You could use an object initializer:
var myInstance = new MyClass
{
Number = 42,
Foo = "Bar"
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With