C# 如何为bool创建1/0返回类型?

C# 如何为bool创建1/0返回类型?,c#,casting,typecasting-operator,C#,Casting,Typecasting Operator,在C#中,如何创建自定义类型/类型转换,即bool?为了更好地解释,我希望这样的表达: bool cond=...; int myVar= cond as customType; 因此,我希望customType表现为myVar变为0或1,具体取决于cond(如果为真,则为1,否则为0) 可能吗 请不要给我条件?1:0解决方案或其他。我问的正是我问的,所以在标记之前重新阅读(或分析)它。如果需要bool(1或0)的值,应该使用GetHashCode函数 我在这里举了一个例子 bool v

在C#中,如何创建自定义类型/类型转换,即
bool
?为了更好地解释,我希望这样的表达:

bool cond=...;
int myVar=  cond as customType;
因此,我希望
customType
表现为
myVar
变为
0
1
,具体取决于
cond
(如果为真,则为1,否则为0)

可能吗



请不要给我条件?1:0解决方案或其他。我问的正是我问的,所以在标记之前重新阅读(或分析)它。

如果需要bool(1或0)的值,应该使用GetHashCode函数

我在这里举了一个例子

bool v1 = true;
bool v2 = false;
int res;

Console.WriteLine($"v1: {v1.GetHashCode()}, v2: {v2.GetHashCode()}");

是的,你可以这样做。如果希望自定义类型的行为类似于内置类型,则必须注意重写从
System.Object
继承的某些方法。此外,还需要转换运算符

让我们创建一个名为
Logical
的结构,它具有
int
属性
Value

public readonly struct Logical : IEquatable<Logical>
{
    public Logical(int value)
    {
        Value = value == 0 ? 0 : 1;
    }

    public Logical(bool cond)
    {
        Value = cond ? 1 : 0;
    }

    public int Value { get; }

    ... conversions and overrides
}
重写
Equals
GetHashCode
也很好,这样可以轻松地比较值或将它们添加到字典或哈希集

public bool Equals(Logical other) // Implements IEquatable<Logical>
{
    return Value.Equals(other.Value);
}

public override bool Equals(object obj)
{
    if (obj is Logical logical) {
        return Equals(logical);
    }
    return base.Equals(obj);
}

public override int GetHashCode() => Value.GetHashCode();
最后,我们重写
ToString
,以便能够打印逻辑

public override string ToString() => Value == 0 ? "FALSE" : "TRUE";
现在,我们可以做这些事情:

double x = 3.14;
Logical logical = x > 0.0;
bool b = logical;

Console.WriteLine($"logical = {logical}");
Console.WriteLine($"b       = {b}");

logical = -3;
Console.WriteLine($"logical = {logical}");
Console.WriteLine($"logical.Value = {logical.Value}");
logical = 0;
Console.WriteLine($"logical = {logical}");
Console.ReadKey();
它打印:

logical=TRUE
b=正确
逻辑=真
逻辑值=1
逻辑=假
你能做的还有很多<例如,code>System.Bool实现了
IComparable
IComparable
IConvertible
IEquatable
。它还有静态的
Parse
TryParse
方法。您还可以使其他操作符过载。请参阅:。

答案可能是您应该使用的答案,除非您有很好的理由不这样做

然而,仅仅因为有趣,您也可以使用以下结构的某个版本,这样转换是隐式的,并且bool值和int值都可以同时使用,而不需要额外的内存使用:

[StructLayout(LayoutKind.Explicit)]
public struct BoolToInt
{
    [FieldOffset(0)]
    public readonly bool booleanValue;

    [FieldOffset(0)]
    public readonly int integerValue;

    public BoolToInt(bool booleanValue)
    {
        this.integerValue = 0; //irrelevant, will be overwritten, but the compiler can't figure this out
        this.booleanValue = booleanValue;
    }
}
如果您想更改布尔值,可以使其可变,但这与结构使用的一般原则背道而驰。

我强烈建议不要使integerValue字段可变。

为什么要引入
自定义类型?为什么不干脆
myVar=cond?1:0
?Convert.ToBoolean(表达式)可能重复?是否正在寻找类似的内容?(带有隐式运算符的自定义类型)您可以定义转换,但它
as
不会调用用户定义的转换正如在我的链接中提到的,布尔类将true实现为整数1,将false实现为整数0。但是,特定的编程语言可能会用其他值表示true和false。而且,
GetHashCode
的实现可能会随时更改。谢谢,请稍后提问,我是否可以直接将该值作为逻辑值使用,它将返回
1
0
。布尔cond
x>0.0
自动转换为
逻辑
。然后可以使用
logical.value
访问该值,或者可以添加转换
公共静态隐式运算符int(logical-logical)=>logical.value和写入
var i=(int)(逻辑)cond其中
cond
是一个
bool
。您还可以将
更改为字符串
,使其返回
“0”
“1”
。您还可以在静态类
publicstaticint-ToLogicalInt(thisboolcond)=>cond中创建一个扩展方法,而不是创建一个类型?1 : 0;并编写
cond.ToLogicalInt()
。感谢您的回答,因此,我可以使用
cond作为BoolToInt
表达式,rigjt?否。如果您想这样做,您必须使用选择的答案。对于这个,您只需使用integerValue属性,不需要强制转换(as)。
double x = 3.14;
Logical logical = x > 0.0;
bool b = logical;

Console.WriteLine($"logical = {logical}");
Console.WriteLine($"b       = {b}");

logical = -3;
Console.WriteLine($"logical = {logical}");
Console.WriteLine($"logical.Value = {logical.Value}");
logical = 0;
Console.WriteLine($"logical = {logical}");
Console.ReadKey();
[StructLayout(LayoutKind.Explicit)]
public struct BoolToInt
{
    [FieldOffset(0)]
    public readonly bool booleanValue;

    [FieldOffset(0)]
    public readonly int integerValue;

    public BoolToInt(bool booleanValue)
    {
        this.integerValue = 0; //irrelevant, will be overwritten, but the compiler can't figure this out
        this.booleanValue = booleanValue;
    }
}