Can we interrupt creating an object in constructor

前端 未结 7 1520
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 03:46

Could you help me please. I have one idea but don\'t know how can I implement it.

So the question is: Can we interrupt creating an object in constructor i.e.

7条回答
  •  佛祖请我去吃肉
    2020-12-31 04:25

    No it is not possible directly.

    You could throw an exception and add the required code to check for the exception and assign null to you variable.

    A better solution would be to use a Factory that would return null if some condition fail.

    var someClass = SomeClassFactory.Create(someCriteria);
    if(someClass != null)
    {
    }
    

提交回复
热议问题