C#中&和&&运算符之间的区别是什么
我试图理解C#中&和&&运算符之间的区别。 我在互联网上搜索没有太大的成功。 有人可以用一个例子来解释一下吗?
&
是按位AND运算符。 对于整数类型的操作数,它将计算操作数的按位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" }
||
与此属性中的&&
类似; 如果第一个操作数的第一个计算结果为false,它将仅计算第二个操作数。
当然,用户定义的类型可以使这些运算符超载,使它们做任何他们想做的事。
强烈推荐Dotnet Mob的这篇文章: http : //codaffection.com/csharp-article/short-circuit-evaluation-in-c/
– &&是短路逻辑运算符
对于AND操作,如果任何操作数计算为false,则总表达式计算为false,因此不需要评估剩余表达式,类似于OR操作,如果任何操作数计算为true,则可以跳过剩余的计算
– &运算符可以用作一元或二元运算符。 也就是说,一元&可用于在不安全的上下文中获取它的操作数的地址。
&是一个按位运算符, &&是一个适用于bool
操作数的逻辑运算符。
&
可以用于整数类型(如int
, long
等)或bool
。
当在整数类型上使用时,它执行按位AND并为您提供结果。 当在bool
上使用时,它会在其操作数上执行逻辑AND并为您提供结果。
&&
不用作按位AND。 它用作逻辑AND,但不一定检查它的操作数。 如果左操作数的计算结果为false,则它不会检查右操作数。
重要的例子:
void Action() { string name = null; if(name != null && name.EndsWith("ack")) { SomeOtherAction(); } }
如果name
为null,则name.EndsWith("ack")
永远不会被执行。 很聪明地知道左操作数是否为假,然后不需要评估右操作数(也称为“短路”)。 这很好,因为在null上调用方法会抛出NullReferenceException。
如果你把它改成了if(name != null & name.EndsWith("ack"))
,那么双方都会得到评估,它会抛出一个NullReferenceException。
一个细节: &
也可以是一个不安全的上下文中的一元运算符(因此它有一个操作数)。 它将给出值或对象的地址。 但这并不重要,因为大多数人都不必触及这部分语言。
下面的例子和解释可能有所帮助。
例:
public static bool Condition1() { return false; } public static bool Condition2() { return true; }
1.逻辑运算符
& (&符号)逻辑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 , 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. }
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. }
注意:
为了更好地理解在控制台示例中测试它。
参考
dotnetmob.com
wikipedia.org
stackoverflow.com
Hai Friend,运算符&&(逻辑运算符)用于条件语句。 例如
if(firstName == 'Tilsan' && lastName == 'Fighter') { Response.Write("Welcome Tilsan The Fighter!"); }
只有当firstName和lastName都与其条件匹配时,Response.Write语句才会运行。
而&(按位运算符)运算符用于二进制AND运算,即,如果我们写:
bool a, b, c; a = true; b = false; c = a & b; Response.Write(c); // 'False' will be written to the web page
这里首先对变量a和b执行二元和运算,结果值将存储在变量c中。
很容易看到它是合乎逻辑的,并且将评估&的两侧,无论左侧是否为假,而短路&&仅在左侧为真时评估右侧。
d=0; n=10; // this throws divide by 0 exception because it evaluates the // mod even though "d != 0" is false if ( (d != 0) & (n % d) == 0 ) Console.Writeline( d + " is a factor of " + n); // This will not evaluate the mod. if ( (d != 0) && (n % d) == 0 ) Console.Writeline( d + " is a factor of " + n);
根据 – C#4.0 Herbert Schildt的完整参考文献
& – 逻辑AND运算符
| – 逻辑OR运算符
&& – 短路和运营商
|| – 短路OR操作员
一个简单的例子可以帮助理解这些现象
using System; class SCops { static void Main() { int n, d; n = 10; d = 2; if(d != 0 && (n % d) == 0) Console.WriteLine(d + " is a factor of " + n); d = 0; // now, set d to zero // Since d is zero, the second operand is not evaluated. if(d != 0 && (n % d) == 0) Console.WriteLine(d + " is a factor of " + n); // Now, try the same thing without the short-circuit operator. // This will cause a divide-by-zero error. if(d != 0 & (n % d) == 0) Console.WriteLine(d + " is a factor of " + n); } }
这里&运算符检查每个操作数,&&仅检查第一个操作数。
正如您可能注意到的’AND’操作一样,任何false的操作数都会将整个表达式计算为false,而不管表达式中的任何其他操作数值。 这种短路forms有助于评估第一部分,并且足够聪明,可以知道第二部分是否必要。
运行程序将为最后一个if条件抛出一个被零除错的错误,其中两个操作数都被检查为&运算符,并且没有进行exception处理以解决’d’在任何时间点都可以为0的事实。
同样的情况适用于’|’ 在C#中。
这与C或C ++略有不同,其中’&’和’|’ 是按位AND和OR运算符。 但是C#也适用于&和|的按位性质 仅适用于int变量。
第一个是按位,第二个是布尔值 。