关于false 运算符的问题

cnjack 2003-07-18 08:52:27
ms-help://MS.VSCC/MS.MSDNVS.2052/csref/html/vclrfFalse.htm
在 C# 中,false 关键字可用作重载运算符或文字值:

false 运算符
false 文本

false 运算符
用户定义类型可以定义 false 运算符,该运算符返回 bool 值 true 以指示 false,否则返回false。这对于表示 true、false 和 null(既非 true 也非 false)的类型很有用,在数据库中使用了该运算符。

这些类型可用于控制 if、do、while 和 for 语句中以及条件表达式中的表达式。

如果类型定义了 false 运算符,则它还必须定义 true 运算符。

类型不能直接重载条件逻辑运算符(&&和 || ),但通过重载规则逻辑运算符和运算符 true 和 false,可以取得相同的效果(请参阅 7.11.2 用户定义的条件逻辑运算符)。

示例
请参阅 11.4.2 数据库布尔类型中的示例。

false 文字值
false 关键字是表示布尔值 false 的 bool 类型的文字值。

示例
// cs_keyword_false.cs
using System;
class test
{
public static void Main()
{
bool a = false;
Console.WriteLine( a ? "yes" : "no" );
}
}
输出
no
请参见

在msdn上看到以上的false运算符,但不明白" 用户定义类型可以定义 false 运算符,该运算符返回 bool 值 true 以指示 false,否则返回false。"这名话的"运算符返回bool值true以指示false"。
...全文
84 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
brightheroes 2003-07-18
  • 打赏
  • 举报
回复
钻牛角了兄弟。你管它说什么,自己用不就好了。
xixigongzhu 2003-07-18
  • 打赏
  • 举报
回复
using System;
public struct DBBool
{
// The three possible DBBool values.
public static readonly DBBool Null = new DBBool(0);
public static readonly DBBool False = new DBBool(-1);
public static readonly DBBool True = new DBBool(1);
// Private field that stores ¨C1, 0, 1 for False, Null, True.
sbyte value;
// Private instance constructor. The value parameter must be ¨C1, 0, or 1.
DBBool(int value) {
this.value = (sbyte)value;
}
// Properties to examine the value of a DBBool. Return true if this
// DBBool has the given value, false otherwise.
public bool IsNull { get { return value == 0; } }
public bool IsFalse { get { return value < 0; } }
public bool IsTrue { get { return value > 0; } }
// Implicit conversion from bool to DBBool. Maps true to DBBool.True and
// false to DBBool.False.
public static implicit operator DBBool(bool x) {
return x? True: False;
}
// Explicit conversion from DBBool to bool. Throws an exception if the
// given DBBool is Null, otherwise returns true or false.
public static explicit operator bool(DBBool x) {
if (x.value == 0) throw new InvalidOperationException();
return x.value > 0;
}
// Equality operator. Returns Null if either operand is Null, otherwise
// returns True or False.
public static DBBool operator ==(DBBool x, DBBool y) {
if (x.value == 0 || y.value == 0) return Null;
return x.value == y.value? True: False;
}
// Inequality operator. Returns Null if either operand is Null, otherwise
// returns True or False.
public static DBBool operator !=(DBBool x, DBBool y) {
if (x.value == 0 || y.value == 0) return Null;
return x.value != y.value? True: False;
}
// Logical negation operator. Returns True if the operand is False, Null
// if the operand is Null, or False if the operand is True.
public static DBBool operator !(DBBool x) {
return new DBBool(-x.value);
}
// Logical AND operator. Returns False if either operand is False,
// otherwise Null if either operand is Null, otherwise True.
public static DBBool operator &(DBBool x, DBBool y) {
return new DBBool(x.value < y.value? x.value: y.value);
}
// Logical OR operator. Returns True if either operand is True, otherwise
// Null if either operand is Null, otherwise False.
public static DBBool operator |(DBBool x, DBBool y) {
return new DBBool(x.value > y.value? x.value: y.value);
}
// Definitely true operator. Returns true if the operand is True, false
// otherwise.
public static bool operator true(DBBool x) {
return x.value > 0;
}
// Definitely false operator. Returns true if the operand is False, false
// otherwise.
public static bool operator false(DBBool x) {
return x.value < 0;
}
public override bool Equals(object obj) {
if (!(obj is DBBool)) return false;
return value == ((DBBool)obj).value;
}
public override int GetHashCode() {
return value;
}
public override string ToString() {
if (value > 0) return "DBBool.True";
if (value < 0) return "DBBool.False";
return "DBBool.Null";
}
public static void Main() {
DBBool db = (DBBool) false;
DBBool db2 = (DBBool) true;
Console.WriteLine(db && db2);
Console.WriteLine(db || db2);
db2 = (DBBool) false;
Console.WriteLine(db && db2);
Console.WriteLine(db || db2);
db = (DBBool) true;
Console.WriteLine(db && db2);
Console.WriteLine(db || db2);
db2 = (DBBool) true;
Console.WriteLine(db && db2);
Console.WriteLine(db || db2);
}
}

xixigongzhu 2003-07-18
  • 打赏
  • 举报
回复
当 && 或 || 的操作数所属的类型声明了适用的用户定义的 operator & 或 operator | 时,下列两个条件必须都为真(其中 T 是声明的选定运算符的类型):

选定运算符的返回类型和每个参数的类型都必须为 T。换言之,该运算符必须计算类型为 T 的两个操作数的逻辑 AND 或逻辑 OR,且必须返回类型为 T 的结果。
T 必须包含关于 operator true 和 operator false 的声明。
如果这两个要求中有一个未满足,则发生编译时错误。如果这两个要求都满足,则通过将用户定义的 operator true 或 operator false 与选定的用户定义的运算符组合在一起来计算 && 或 || 运算:

x && y 运算按 T.false(x) ? x : T.&(x, y) 进行计算,其中 T.false(x) 是 T 中声明的 operator false 的调用,T.&(x, y) 是选定 operator & 的调用。换言之,首先计算 x,并对结果调用 operator false 以确定 x 是否肯定为假。然后,如果 x 肯定为假,则运算结果为先前为 x 计算的值。否则将计算 y,并对先前为 x 计算的值和为 y 计算的值调用选定的 operator & 以产生运算结果。
x || y 运算按 T.true(x) ? x : T.|(x, y) 进行计算,其中 T.true(x) 是 T 中声明的 operator true 的调用,T.|(x, y) 是选定 operator | 的调用。换言之,首先计算 x,并对结果调用 operator true 以确定 x 是否肯定为真。然后,如果 x 肯定为真,则运算结果为先前为 x 计算的值。否则将计算 y,并对先前为 x 计算的值和为 y 计算的值调用选定的 operator | 以产生运算结果。
在这两个运算中,x 给定的表达式只计算一次,y 给定的表达式要么不计算,要么只计算一次。
AhBian 2003-07-18
  • 打赏
  • 举报
回复
你有什么问题啊?怎么不见问题呢。

false 运算符可以理解为,判断一个逻辑值是否为 false,如果是的话,返回的结果是 true;否则,返回 false。

浅显一点的说, false 运算符相当于 bool IsFalse(bool) 方法,用来判断一下值是不是 false。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧