gpt4 book ai didi

c# - 以下这两种方法有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-03 21:27:44 26 4
gpt4 key购买 nike

我有以下代码片段。

bool b = false;
if (b) {}

但是我看到很多人写这样的话:

if (true == b){}

它们在我看来都一样,这里有什么不同吗?

最佳答案

bool b;
if (b) {}

您不能使用它,因为 C# 编译器不允许使用未分配的局部变量。

另一方面,两者没有区别

bool b = true;
if (b) {}

bool b = true;
if (true == b){}

它们生成相同的 MSIL代码也是如此。但在我看来,第二个例子中的相等性检查是不必要的。这就是为什么 if(b) 看起来比 if(true == b) 更干净。

关于c# - 以下这两种方法有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25992412/

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