C#学习基本概念(3003)--运算符位运或算符| 、计算操作说的逻辑或|、条件或运算符||
2016-12-22 13:19
453 查看
运算符:
Binary
备注
用户定义的类型可重载
***************************************************
条件或运算符||
条件或运算符 (
操作
条件或运算符无法重载,但是,公共逻辑运算符和 真 和 假 运算符的重载,有一些限制的,也将条件逻辑运算符的重载
示例
在下面的示例中,表达式中使用的
备注:转自https://msdn.microsoft.com/zh-cn/library/6373h346.aspx
Binary
|运算符是为整型和
bool预定义的。 对于整型,
|计算操作数的按位“或”。 对于
bool操作数,
|计算操作数的逻辑“或”;也就是说,当且仅当两个操作数均为
false时,结果才为
false。
备注
用户定义的类型可重载
|运算符(请参见运算符)。
class OR { static void Main() { Console.WriteLine(true | false); // logical or Console.WriteLine(false | false); // logical or Console.WriteLine("0x{0:x}", 0xf8 | 0x3f); // bitwise or } } /* Output: True False 0xff */备注:转自:
***************************************************
条件或运算符||
条件或运算符 (
||) 执行的逻辑或其
bool操作数。 如果第一个操作数计算结果为
true,第二个操作数对象不会计算。 如果第一个操作数计算结果为
false,第二个运算符确定或表达式整体是否计算结果为
true或
false。
操作
x || y对应于操作
x | y但,如果
x是
true,
y不会计算无论
y,的值,因为或操作是
true。 此概念称为“短路计算”。
条件或运算符无法重载,但是,公共逻辑运算符和 真 和 假 运算符的重载,有一些限制的,也将条件逻辑运算符的重载
示例
在下面的示例中,表达式中使用的
||计算只有第一个操作数。 使用的表达式
|计算两个操作数。 在第二个示例中,因此,如果两个操作数计算,则运行时会发生异常。
class ConditionalOr { // Method1 returns true. static bool Method1() { Console.WriteLine("Method1 called."); return true; } // Method2 returns false. static bool Method2() { Console.WriteLine("Method2 called."); return false; } static bool Divisible(int number, int divisor) { // If the OR expression uses ||, the division is not attempted // when the divisor equals 0. return !(divisor == 0 || number % divisor != 0); // If the OR expression uses |, the division is attempted when // the divisor equals 0, and causes a divide-by-zero exception. // Replace the return statement with the following line to // see the exception. //return !(divisor == 0 | number % divisor != 0); } static void Main() { // Example #1 uses Method1 and Method2 to demonstrate // short-circuit evaluation. Console.WriteLine("Regular OR:"); // The | operator evaluates both operands, even though after // Method1 returns true, you know that the OR expression is // true. Console.WriteLine("Result is {0}.\n", Method1() | Method2()); Console.WriteLine("Short-circuit OR:"); // Method2 is not called, because Method1 returns true. Console.WriteLine("Result is {0}.\n", Method1() || Method2()); // In Example #2, method Divisible returns True if the // first argument is evenly divisible by the second, and False // otherwise. Using the | operator instead of the || operator // causes a divide-by-zero exception. // The following line displays True, because 42 is evenly // divisible by 7. Console.WriteLine("Divisible returns {0}.", Divisible(42, 7)); // The following line displays False, because 42 is not evenly // divisible by 5. Console.WriteLine("Divisible returns {0}.", Divisible(42, 5)); // The following line displays False when method Divisible // uses ||, because you cannot divide by 0. // If method Divisible uses | instead of ||, this line // causes an exception. Console.WriteLine("Divisible returns {0}.", Divisible(42, 0)); } } /* Output: Regular OR: Method1 called. Method2 called. Result is True. Short-circuit OR: Method1 called. Result is True. Divisible returns True. Divisible returns False. Divisible returns False. */
备注:转自https://msdn.microsoft.com/zh-cn/library/6373h346.aspx
相关文章推荐
- C#学习基本概念(3001)---运算符
- C#学习基本概念(3004)---&、&& 与运算符
- C#网络编程(基本概念和操作) - Part.1
- C#及.NET2.0学习笔记之C#语言基本概念
- java线程学习1——线程基本概念和操作
- C#网络编程(基本概念和操作) - Part.1
- C# 逻辑与条件运算符
- [转]C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- 第一天,第一个C#程序,学习C#基本语法。掌握命名空间、类、变量、方法等基本概念。
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1[转自JimmyZhang博客]
- C#网络编程(基本概念和操作) - Part.1
- 【分布计算环境学习笔记】1 基本概念和发展历史
- java线程学习1——线程基本概念和操作