C#: if a class has two constructors, what is the best way for these constructors to share some code?

c#, constructor

Solution

Just chain to the common constructor...

public MyObject(int setZ)
  : this()
{
  z = setZ;
}

Problem

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; } ```

Original source

Related problems