C# | 之间有什么区别?和 || 或运营商?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:16:05  来源:igfitidea点击:

What is the difference between the | and || or operators?

提问by GloryFish

I have always used ||(two pipes) in OR expressions, both in C# and PHP. Occasionally I see a single pipe used: |. What is the difference between those two usages? Are there any caveats when using one over the other or are they interchangeable?

我一直||在 C# 和 PHP 中的 OR 表达式中使用(两个管道)。有时我会看到使用单个管道:|. 这两种用法有什么区别?在使用一个或另一个时是否有任何警告或它们可以互换?

采纳答案by Michael Stum

Just like the &and &&operator, the double Operator is a "short-circuit" operator.

就像&and&&运算符一样,双运算符是一个“短路”运算符。

For example:

例如:

if(condition1 || condition2 || condition3)

If condition1 is true, condition 2 and 3 will NOT be checked.

如果条件 1 为真,则不会检查条件 2 和 3。

if(condition1 | condition2 | condition3)

This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.

这将检查条件 2 和 3,即使 1 已经为真。由于您的条件可能是非常昂贵的函数,因此您可以通过使用它们来获得良好的性能提升。

There is one big caveat, NullReferences or similar problems. For example:

有一个很大的警告,NullReferences 或类似的问题。例如:

if(class != null && class.someVar < 20)

If class is null, the if-statement will stop after class != nullis false. If you only use &, it will try to check class.someVarand you get a nice NullReferenceException. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.

如果 class 为 null,则 if 语句将在class != null为 false后停止。如果您只使用 &,它会尝试检查class.someVar并得到一个不错的NullReferenceException. 使用 Or-Operator 可能不是一个陷阱,因为您不太可能触发坏事,但需要牢记这一点。

No one ever uses the single &or |operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The &operator does "run these 3 functions, and if one of them returns false, execute the else block", while the |does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.

但是,没有人使用 single&|运算符,除非您的设计中每个条件都是必须执行的函数。听起来像是设计的味道,但有时(很少)这是一种干净的做事方式。该&操作者“跑这3个功能,如果其中一个返回false,执行else块”,而|不会“只运行else块,如果没有回报假” -可能是有用的,但正如所说,往往这是一个设计闻。

There is a Second use of the |and &operator though: Bitwise Operations.

|&运算符还有第二个用法:按位运算

回答by nsanders

One is a "bitwise or".

一种是“按位或”。

10011b | 01000b => 11011b

10011b | 01000b => 11011b

The other is a logic or.

另一个是逻辑或。

true or false => true

真或假 => 真

回答by homeskillet

The | operator performs a bitwise OR of its two operands (meaning both sides must evaluate to false for it to return false) while the || operator will only evaluate the second operator if it needs to.

的| 运算符对其两个操作数执行按位或运算(意味着双方必须评估为假才能返回假),而 || 运算符只会在需要时评估第二个运算符。

http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

回答by Kyle Cronin

The singe pipe "|" is the "bitwise" or and should only be used when you know what you're doing. The double pipe "||" is a logical or, and can be used in logical statements, like "x == 0 || x == 1".

单管“|” 是“按位” or 并且只应在您知道自己在做什么时使用。双管“||” 是逻辑或,可用于逻辑语句,如“x == 0 || x == 1”。

Here's an example of what the bitwise or does: if a=0101 and b=0011, then a|b=0111. If you're dealing with a logic system that treats any non-zero as true, then the bitwise or will act in the same way as the logical or, but it's counterpart (bitwise and, "&") will NOT. Also the bitwise or does not perform short circuit evaluation.

下面是按位或执行的示例:如果 a=0101 和 b=0011,则 a|b=0111。如果您正在处理将任何非零视为真的逻辑系统,那么按位 or 的行为方式与逻辑 or 相同,但它的对应项(按位和,“&”)不会。此外按位或不执行短路评估。

回答by Dane

A single pipe (|) is the bitwise OR operator.

单个管道 (|) 是按位 OR 运算符

Two pipes (||) is the logical OR operator.

两个管道 (||) 是逻辑 OR 运算符。

They are not interchangeable.

它们不可互换。

回答by Evan Shaw

|| is the logical OR operator. It sounds like you basically know what that is. It's used in conditional statements such as if, while, etc.

|| 是逻辑 OR 运算符。听起来你基本上知道那是什么。它用于条件语句,例如 if、while 等。

condition1 || condition2

Evaluates to true if either condition1 OR condition2 is true.

如果条件 1 或条件 2 为真,则评估为真。

| is the bitwise OR operator. It's used to operate on two numbers. You look at each bit of each number individually and, if one of the bits is 1 in at least one of the numbers, then the resulting bit will be 1 also. Here are a few examples:

| 是按位 OR 运算符。它用于对两个数字进行运算。您单独查看每个数字的每一位,如果其中一个位在至少一个数字中为 1,则结果位也将为 1。这里有一些例子:

A = 01010101
B = 10101010
A | B = 11111111

A = 00000001
B = 00010000
A | B = 00010001

A = 10001011
B = 00101100

A | B = 10101111

Hopefully that makes sense.

希望这是有道理的。

So to answer the last two questions, I wouldn't say there are any caveats besides "know the difference between the two operators." They're not interchangeable because they do two completely different things.

因此,要回答最后两个问题,除了“了解两个运营商之间的区别”之外,我不会说有任何警告。它们不可互换,因为它们做两件完全不同的事情。

回答by Trevor Abell

Good question. These two operators work the same in PHP and C#.

好问题。这两个运算符在 PHP 和 C# 中的工作方式相同。

|is a bitwise OR. It will compare two values by their bits. E.g. 1101 | 0010 = 1111. This is extremely useful when using bit options. E.g. Read = 01 (0X01) Write = 10 (0X02) Read-Write = 11 (0X03). One useful example would be opening files. A simple example would be:

|是按位或。它将按位比较两个值。例如 1101 | 0010 = 1111。这在使用位选项时非常有用。例如读取 = 01 (0X01) 写入 = 10 (0X02) 读写 = 11 (0X03)。一个有用的例子是打开文件。一个简单的例子是:

File.Open(FileAccess.Read | FileAccess.Write);  //Gives read/write access to the file

||is a logical OR. This is the way most people think of OR and compares two values based on their truth. E.g. I am going to the store or I will go to the mall. This is the one used most often in code. For example:

||是逻辑 OR。这是大多数人认为 OR 并根据其真实性比较两个值的方式。例如,我要去商店或去商场。这是代码中最常用的一种。例如:

if(Name == "Admin" || Name == "Developer") { //allow access } //checks if name equals Admin OR Name equals Developer

PHP Resource: http://us3.php.net/language.operators.bitwise

PHP 资源:http: //us3.php.net/language.operators.bitwise

C# Resources: http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

C# 资源:http: //msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

回答by codeLes

The single pipe, |, is one of the bitwiseoperators.

单个管道 | 是按位运算符之一。

From Wikipedia:

来自维基百科:

In the C programming language family, the bitwise OR operator is "|" (pipe). Again, this operator must not be confused with its Boolean "logical or" counterpart, which treats its operands as Boolean values, and is written "||" (two pipes).

在 C 编程语言家族中,按位 OR 运算符是“|” (管道)。同样,此运算符不能与其布尔“逻辑或”对应物混淆,后者将其操作数视为布尔值,并写作“||” (两根管子)。

回答by vishesh

Simple example in java

java中的简单例子

public class Driver {

  static int x;
  static int y;

public static void main(String[] args) 
throws Exception {

System.out.println("using double pipe");
    if(setX() || setY())
        {System.out.println("x = "+x);
        System.out.println("y = "+y);
        }



System.out.println("using single pipe");
if(setX() | setY())
    {System.out.println("x = "+x);
    System.out.println("y = "+y);
    }

}

 static boolean setX(){
      x=5;
     return true;
  }
 static boolean setY(){
      y=5;
      return true;
  }
}

output :

输出 :

using double pipe
x = 5
y = 0
using single pipe
x = 5
y = 5

回答by Dragos Bandur

By their mathematical definition, OR and AND are binary operators; they verify the LHS and RHS conditions regardless, similarly to | and &.

根据它们的数学定义,OR 和 AND 是二元运算符;无论如何,他们验证 LHS 和 RHS 条件,类似于 | 和 &。

|| and && alter the properties of the OR and AND operators by stopping them when the LHS condition isn't fulfilled.

|| and && 通过在 LHS 条件不满足时停止它们来改变 OR 和 AND 运算符的属性。