I am applying validation logic in the constructor and returning if that fails. But, still the instance is created. How can i make sure that if a logic fails, the ctor should not create the instance.
class Car
{
public readonly int maxSpeed;
private int currSpeed = 99;
public Car(int max)
{
if (max > 50)
return;
}
public Car()
{
maxSpeed = 55;
}
}
You should throw an exception in the constructor if validation fails.
Various framework classes do that, look at the System.String or System.DateTime constructors for example.