How I can use constructors in C# like this:
public Point2D(double x, double y)
{
// ... Contracts ...
X = x;
Y = y;
}
public Point2D(Point2D point)
{
if (point == null)
ArgumentNullException("point");
Contract.EndContractsBlock();
this(point.X, point.Y);
}
I need it to not copy code from another constructor...
You can factor out your common logic to a private method, for example called
Initialize
that gets called from both constructors.Due to the fact that you want to perform argument validation you cannot resort to constructor chaining.
Example:
Maybe your class isn't quite complete. Personally, I use a private init() function with all of my overloaded constructors.