Will an IF statement stop evaluating if it fails the first condition?(如果 IF 语句不满足第一个条件,它会停止评估吗?)
问题描述
如果我有一个带有 2 个条件的 If
语句 - 第一个条件失败,是否会考虑第二个条件,还是会直接进入 else
?因此,在以下示例中,如果 myList.Count == 0
,将 myString
与值"进行比较还是直接与 else
?
If I have an If
statement with 2 conditions - and the first fails, will the 2nd condition even be considered or will it go straight to the else
? So, in the following example, if myList.Count == 0
, will myString
be compared against "value" or will it just straight to else
?
if(myList.Count > 0 && myString.Equals("value"))
{
//Do something
}
else
{
//Do something else
}
推荐答案
它将停止评估,因为您使用的是双与号 &&操作员.这称为 短路.
It will stop evaluating because you're using the double ampersand && operator. This is called short-circuiting.
如果您将其更改为单个 & 符号:
If you changed it to a single ampersand:
if(myList.Count > 0 & myString.Equals("value"))
它会同时评估两者.
这篇关于如果 IF 语句不满足第一个条件,它会停止评估吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果 IF 语句不满足第一个条件,它会停止评估吗?


- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01