如何在C#中使用两个或多个对象? [英] how to use or for two or more objects in C#?

查看:287
本文介绍了如何在C#中使用两个或多个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
我想知道如何在以下方面使用或喜欢使用它:
如果某件事或其他事情发生或不发生.....
如果(.. ll .....)
{

}
如何在C#中使用它?
已删除电子邮件[/编辑]

hi.
i want to know how can i use or like using it in:
if some thing or something else happened or not do .... .
if (.... ll .....)
{

}
how can i use it in C#??
Removed Email[/Edit]

推荐答案

有两个布尔或"运算符:"|"和"||".第一个在所有情况下都检查左和右布尔操作数,第二个检查"||"优化检查:如果第一个操作数为true,则无需检查第二个操作数.

为什么不使用"||"每时每刻?好吧,这就是我要做的.但是"|"如果第二个布尔操作数是具有副作用的函数,则可能需要.如果使用"||"优化了呼叫,则副作用将消失.运算符"|"不允许.

示例:

There are two Boolean "OR" operators: "|" and "||". First one checks up both left and right Boolean operands in all cases, the second one "||" optimizes the check: if first operand is true, there is no need to check up the second operand.

Why not using "||" all the time? Well, this is what I do. But "|" can be needed in case the second Boolean operand is a function with the side effect. If the call is optimized out using "||", the side effect will disappear. The operator "|" will not allow it.

Example:

bool Right() {
   bool result = //... calculate the result
   System.Console.WriteLine("Right returned {0}", result); //this lines does side effect
   return result;
}

//...

bool left = true;

//...

if (left || Right())
   //...
   // the result of Right will never be printed for left == true;

//...

if (left | Right())
   //...
   // the result of Right will always be printed



操作数的一个非常典型的布尔表达式是算术比较表达式:



A very typical Boolean expression for the operand is arithmetic comparison expression:

double a, start, end;

//...

if (a < start || a > end)
   System.Console.WriteLine("The value {0} is outside the segment [{1} .. {2}]", a, start, end);



与布尔值&&"相同和&"运营商.运算符&&"如果左操作数为false,将跳过对右操作数的检查.

不要混淆布尔值"|"和二进制(集合)运算符"|";不要混淆布尔值&"和二进制(集合)运算符&".

—SA



Same thing with Boolean "&&" and "&" operators. The operator "&&" will skip checking of the right operand if the left operand is false.

Don''t mix up Boolean "|" and binary (set) operator "|"; don''t mix up Boolean "&" and binary (set) operator "&".

—SA


这篇关于如何在C#中使用两个或多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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