gpt4 book ai didi

c# - 快速退出方法或构造函数有什么好处吗?

转载 作者:太空狗 更新时间:2023-10-30 00:03:25 24 4
gpt4 key购买 nike

我认为这部分与短路逻辑有关,但我找不到任何直接回答我问题的问题。可能的相关问题:Benefits of using short-circuit evaluation , Why use short-circuit code?

考虑以下两个代码块,它们都是类的可能构造函数

public MyClass(OtherClass other){
if (other != null) {
//do something with other, possibly default values in this object
}
}

还有这个

public MyClass(OtherClass other){
if (other == null) return;

//do something with other, possibly default values in this object
}

做后者比做前者有什么好处吗?构造函数中没有其他代码,只有使用 other 对象构造此对象的代码。

最佳答案

在这种情况下,您应该做对您和您的同事来说最易读的事情。两种方法之间不太可能存在任何可察觉的速度差异。

关于c# - 快速退出方法或构造函数有什么好处吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9910505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com