简体   繁体   English

C#中的真假运算符是什么?

[英]What are true and false operators in C#?

What is the purpose and effect of the true and false operators in C#? 什么是的目的和效果truefalse在C# 运营商 The official documentation on these is hopelessly non-explanatory. 关于这些的官方文件是绝对无法解释的。

You would overload the true or false operators if you were defining a specialized boolean value. 如果要定义专门的布尔值,则会重载truefalse运算符。 This is not typically needed, however, which is why they don't seem useful. 然而,这通常不是必需的,这就是为什么它们看起来没用。 For example, in a fuzzy-logic boolean class, you might have something like this: 例如,在模糊逻辑布尔类中,您可能具有以下内容:

// Represents a boolean that can store truth values in a range from [0, 1], with
//   a value of one indicating complete metaphysical certitude and a value of
//   zero indicating complete impossibility.
public class FuzzyBoolean {
// ...
   public static bool operator true(FuzzyBoolean fb) {
      return fb.TruthValue > 0;
   }

   public static bool operator false(FuzzyBoolean fb) {
      return fb.TruthValue == 0;
   }
// ...
}

Note that if you overload true , you must also overload false (and vice versa). 请注意,如果重载为true ,则还必须重载false (反之亦然)。

Of course, there are also the true and false literals , the two literal values you can assign to a boolean instance. 当然,还有truefalse 文字 ,你可以分配给布尔实例的两个文字值。 Don't confuse these with the operators mentioned above. 不要将这些与上面提到的操作员混淆。 A more substantial example of how you'd use this, involving booleans in a database, is given in the MSDN docs here . 你会如何使用这一点,涉及布尔数据库中的一个较大幅度的例子,在MSDN文档中给出这里

The true and false operators can be overloaded, to allow a class to represent its own state as true or false, for example: 可以重载true和false运算符,以允许类将其自己的状态表示为true或false,例如:

public class MyClass
{
    //...
    public static bool operator true(MyClass op)
    {
        // Evaluation code...
    }

    public static bool operator false(MyClass op)
    {
        // Evaluation code...
    }
}

And you will be able to use the operator in boolean expressions: 并且您将能够在布尔表达式中使用运算符:

MyClass test = new MyClass(4, 3);
if (test)
    Console.WriteLine("Something true");
else
    Console.WriteLine("Something false");

string text = test ? "Returned true" : "Returned false";

See the referenced example in the article 请参阅文章中引用的示例

C# Language Specification -- Database boolean type C#语言规范 - 数据库布尔类型

Essentially these operators allow an instance of a type to be used in boolean conditional logic such as && and || 本质上,这些运算符允许在布尔条件逻辑中使用类型的实例,例如&&|| .

Prior to C# 2.0, the true and false operators were used to create user-defined nullable value types that were compatible with types such as SqlBool. 在C#2.0之前,使用true和false运算符创建与SqlBool等类型兼容的用户定义的可空值类型。 However, the language now provides built-in support for nullable value types, and whenever possible you should use those instead of overloading the true and false operators. 但是,该语言现在提供对可空值类型的内置支持,并且只要有可能,您应该使用它们而不是重载true和false运算符。 For more information.With nullable Booleans, the expression a != b is not necessarily equal to !(a == b) because one or both of the values might be null. 有关更多信息。使用可空的布尔值,表达式a!= b不一定等于!(a == b),因为其中一个或两个值可能为空。 You need to overload both the true and false operators separately to correctly identify the null values in the expression. 您需要分别重载true和false运算符,以正确识别表达式中的空值。 The following example shows how to overload and use the true and false operators. 以下示例显示如何重载和使用true和false运算符。

它们允许您使用运算符重载语法重载它们,以便您定义的类型可以解释为布尔值。

The allow you to use a custom type as a part of logic operations; 允许您使用自定义类型作为逻辑运算的一部分; For example, as part of an if or while statement. 例如,作为if或while语句的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM