多条件射击顺序 [英] Multiple Conditions Firing order

查看:56
本文介绍了多条件射击顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有谁知道这两个代码段是否会以相同的方式运行。


例如,如果第一个条件失败难以检查

秒吗?


if(MyList.Count 0&& CheckDate){


//做点什么...


}





if(b) MyList.Count)

{

if(CheckDate)

{

//做点什么...... < br $>
}

}


公布bool CheckDate()

{

返回true;

}

Hi,

Does anyone know if these 2 code segments would operate the same way.

for instance if the first condition fails does it bother to check the
second?

if (MyList.Count 0 && CheckDate) {

// do something...

}

or

if(MyList.Count)
{
if(CheckDate)
{
// do something...
}
}

public bool CheckDate()
{
return true;
}

推荐答案

在C#中,与C一样,逻辑是短路的。所以当你有if(a&& b)

时它只会检查b是否为真,如果你有if(a || b)它

只检查b是否为真;否则它将在

检查后返回。这就是为什么你可以逃脱if(object!= null&&

object.Property == something)。在VB中,A​​nd和Or逻辑不是短路,你必须使用AndAlso和OrElse。


说到这个,我会假设这两个是等价的,但可能会有一个微妙的区别,我会推迟给其他人。

3月26日下午6:31,TheLostLeaf ; < e ... @ canyondigital.comwrote:
In C#, like C, logic is short-circuiting. So when you have if(a && b)
it will only check b if a is true, and when you have if(a || b) it
will only check b if a is NOT true; otherwise it will return after
checking a. That''s why you can get away with if(object != null &&
object.Property == something). In VB, the And and Or logic is NOT
short-circuting, you have to use AndAlso and OrElse for this.

With that said, I would assume the two are equivalent but there could
be a subtle difference, I will defer to somebody else on that.
On Mar 26, 6:31 pm, "TheLostLeaf" <e...@canyondigital.comwrote:




有谁知道这两个代码段是否会以同样的方式操作。


例如,如果第一个条件失败,那么它是否需要检查

秒?


if(MyList.Count 0&& CheckDate){


//做点什么......


}





if(MyList.Count)

{

if(CheckDate)< br $>
{

//做点什么...

}


}


public bool CheckDate()

{

返回true;


} - 隐藏引用文字 -


- 显示引用文字 -
Hi,

Does anyone know if these 2 code segments would operate the same way.

for instance if the first condition fails does it bother to check the
second?

if (MyList.Count 0 && CheckDate) {

// do something...

}

or

if(MyList.Count)
{
if(CheckDate)
{
// do something...
}

}

public bool CheckDate()
{
return true;

}- Hide quoted text -

- Show quoted text -



谢谢!


我想仔细检查,但这是有道理的。更少的代码总是更好地获得相同的结果。

Thanks !

I wanted to double check, but that makes sense. Less code is always
nicer to get the same result.


TheLostLeaf< er ** @ canyondigital.comwrote:
TheLostLeaf <er**@canyondigital.comwrote:

谢谢!


我想仔细检查,但这是有道理的。更少的代码总是

更好,以获得相同的结果。
Thanks !

I wanted to double check, but that makes sense. Less code is always
nicer to get the same result.



请注意,只有&&和||是短路的。如果您将

条件更改为


if(MyList.Count 0& CheckDate)


它将总是测试两个条件。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

Note that only && and || are short-circuiting. If you change your
condition to

if (MyList.Count 0 & CheckDate)

it will always test both conditions.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于多条件射击顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆