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 语句不满足第一个条件,它会停止评估吗?


基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01