C# in VS2005: if a class has two constructors, what is the best way for these constructors to share some code?
eg. How could I avoid having the x = 5
and y = 10
lines in both constructors for the following:
public class MyObject { int x; int y; int z; public MyObject() { x = 5; y = 10; } public MyObject(int setZ) { x = 5; y = 10; z = setZ; }
Just chain to the common constructor...
public MyObject(int setZ) : this() { z = setZ; }
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