“如果"语句-对象的验证顺序? [英] "If" statement - Order of validation of objects?
问题描述
关于C#.NET中IF语句的顺序的简单问题
if(Company!= null&& Company.ID> 0){}
C#是否从左到右工作,因此对Company.ID的检查有效吗?
谢谢.
从本质上讲,是的.
if
语句在括号内需要一个布尔运算符,以确定对下一条语句的评估.
使用&&
运算符,如果第一个布尔检查 Company!= null
为false,它将声明返回false,而不执行另一个( Company.ID> 0
).
(供参考)使用 ||
运算符将在第一个语句之后返回true(如果为true),并且不对第二个语句求值.
Simple question about the order of an IF statement in C#.NET
if (Company !=null && Company.ID > 0)
{
}
Does C# work from left to right, and therefore make the check on Company.ID valid?
Thanks.
Essentially, yes.
The if
statement expects a boolean operator within the parentheses to determine evaluation of the next statement.
Using the &&
operator, if the first boolean check Company !=null
is false, it will statement returning false, and not execute the other (Company.ID > 0
).
Also (for reference), using the ||
operator will return true after the first statement if it is true and not evaluate the second.
这篇关于“如果"语句-对象的验证顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!