C#中&和&&运算符之间的区别是什么

Bre*_*ead 28 c#

我试图理解C#中&和&&运算符之间的区别.我在互联网上搜索没有太大的成功.有人可以用一个例子解释一下吗?

Meh*_*ari 40

&是按位AND运算符.对于整数类型的操作数,它将计算操作数的按位与 - 并且结果将是整数类型.对于布尔操作数,它将计算逻辑和操作数.&&是逻辑AND运算符,不适用于整数类型.对于布尔类型,可以应用它们两者,区别在于"短路"属性&&.如果&&求值的第一个操作数,false则根本不评估第二个操作数.情况并非如此&:

 bool f() {
    Console.WriteLine("f called");
    return false;
 }
 bool g() {
    Console.WriteLine("g called");
    return false;
 }
 static void Main() {
    bool result = f() && g(); // prints "f called"
    Console.WriteLine("------");
    result = f() & g(); // prints "f called" and "g called"
 }
Run Code Online (Sandbox Code Playgroud)

||&&此属性类似; 如果第一个操作数的第一个计算结果为false,它将仅计算第二个操作数.

当然,用户定义的类型可以使这些运算符超载,使它们做任何他们想做的事.


Sha*_*r K 7

强烈推荐Dotnet Mob的这篇文章:http://codaffection.com/csharp-article/short-circuit-evaluation-in-c/

- &&是短路逻辑运算符

对于AND操作,如果任何操作数计算为false,则总表达式计算为false,因此不需要评估剩余表达式,类似于OR操作,如果任何操作数计算为true,则可以跳过剩余的计算

- &运算符可以用作一元或二元运算符.也就是说,一元&可用于在不安全的上下文中获取它的操作数的地址.


Bur*_*rak 7

& can be used on either integral types (like int, long etc.) or on bool.

When used on an integral type, it performs a bitwise AND and gives you the result of that. When used on a bool, it performs a logical AND on BOTH its operands and gives you the result.

&& is not used as a bitwise AND. It is used as a logical AND, but it does not necessarily check both its operands. If the left operand evaluates to false, then it doesn't check the right operand.

Example where this matters:

void Action() 
{
    string name = null;
    if(name != null && name.EndsWith("ack"))
    {
        SomeOtherAction();
    }
}
Run Code Online (Sandbox Code Playgroud)

If name is null, then name.EndsWith("ack") will never get executed. It is smart enough to know if the left operand is false, then the right operand doesn't need to be evaluated (aka "short-circuiting"). That's good because calling a method on null will throw a NullReferenceException.

If you changed it into if(name != null & name.EndsWith("ack")), both sides would get evaluated and it would throw a NullReferenceException.

One detail: & can also be a unary operator (so it has one operand) in an unsafe context. It will give the address of a value or object. It's not important though, as most people don't ever have to touch this part of the language.


sto*_*tom 6

下面的示例和解释可能会有所帮助。

例子:

    public static bool Condition1()
    {
        Console.WriteLine("Condition1 is evaluated.");
        return false;
    }

    public static bool Condition2()
    {
        Console.WriteLine("Condition2 is evaluated.");
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

1. 逻辑运算符

&(与号)逻辑与运算符

| (管道)逻辑或运算符

用于确保计算所有操作数。

if(Condition1() & Condition2())
{
  Console.WriteLine("This will not print");

  //because if any one operand evaluated to false ,  
  //thus total expression evaluated to false , but both are operand are evaluated.
}

 if (Condition2() | Condition1())
 {
   Console.WriteLine("This will print");

   //because any one operand evaluated to true ,  
  //thus total expression evaluated to true , but both are operand are evaluated.
 }
Run Code Online (Sandbox Code Playgroud)

2. 条件短路算子

&&(双与号)条件 AND 运算符

|| (双管道)条件 OR 运算符

用于跳过右侧操作数,有副作用,请谨慎使用

if (Condition1() && Condition2())
{
   Console.WriteLine("This will not print");

   //because if any one operand evaluated to false,
   //thus total expression evaluated to false , 
   //and here the side effect is that second operand is skipped 
   //because first operand evaluates to false.
}

if (Condition2() || Condition1())
{
   Console.WriteLine("This will print");

  //because any one operand evaluated to true 
  //thus remaining operand evaluations can be skipped.
}
Run Code Online (Sandbox Code Playgroud)

笔记:

为了更好地理解,请在控制台示例中测试它。

参考

dotnetmob.com

维基百科.org

stackoverflow.com


Kla*_*sen 1

第一个是按位,第二个是布尔值 and