假设我有一个函数需要返回一些整数值。但它也可能会失败,我需要知道它何时会失败。
哪种方法更好?
public int? DoSomethingWonderful()
或
public bool DoSomethingWonderful(out int parameter)
这可能更像是一个风格问题,但我仍然很好奇人们会采取哪种选择。
编辑:澄清一下,这段代码与一个黑盒子对话(我们称它为云。不,黑盒子。不,等等。云。是的)。我不在乎为什么它失败了。我只需要知道我是否具有有效值。
最佳答案
我更喜欢可空版本,因为您可以使用空合并运算符??在上面,例如:
int reallyTerrible = 0;
var mightBeWonderful = DoSomethingWonderful() ?? reallyTerrible;
关于c# - 哪个更好,使用可为 null 或 bool return+out 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1800003/