我在构造函数中应用验证逻辑,如果失败则返回.但是,仍然创建了实例.如何确保如果逻辑失败,ctor不应该创建实例.
class Car
{
public readonly int maxSpeed;
private int currSpeed = 99;
public Car(int max)
{
if (max > 50)
return;
}
public Car()
{
maxSpeed = 55;
}
}
Run Code Online (Sandbox Code Playgroud)