【发布时间】:2020-06-25 16:42:48
【问题描述】:
假设我有一个包含布尔值列表的类,并且我想要一个属性来报告它们是否都是“真”。有几种方法可以做到这一点:
class BunchOfBools
{
List<bool> Stuff = new List<bool>();
bool AllAreTrue1 => Stuff.All(b => b);
bool AllAreTrue2 => Stuff.TrueForAll(b => b);
}
酷。生活是美好的。
但现在我意识到我有一个实现“operator true”和“operator false”的类
class BatteryIsGood
{
float BatteryVoltage;
public static bool operator true(BatteryIsGood ab) => ab.BatteryVoltage >= 3;
public static bool operator false(BatteryIsGood ab) => ab.BatteryVoltage < 3;
}
如果我有一堆“BatteryIsGood”对象(是的,这是一个人为的例子)并且我想使用 BunchOfBools 来检查它们是否都很好 - 所以让 BunchOfBools 通用:
class BunchOfBools<T>
{
List<T> Stuff = new List<T>();
bool AllAreTrue1 => Stuff.All(b => b);
bool AllAreTrue2 => Stuff.TrueForAll(b => b);
bool AllAreTrue3 => Stuff.All(b => (bool)b);
bool AllAreTrue4 => Stuff.TrueForAll(b => (bool)b);
bool AllAreTrue5
{
get
{
foreach (T one in Stuff)
{
if (!one)
return false;
}
return true;
}
}
bool AllAreTrue6 => Stuff.All(b => (bool)(object)b);
bool AllAreTrue7 => Stuff.TrueForAll(b => (bool)(object)b);
}
BunchOfBools<BatteryIsGood> allBatteriesAreGood;
当然,编译器对泛型 BunchOfBools(函数 1 到 5 无法编译)一点也不满意,因为“无法将类型 'T' 转换为 bool”。 AllAreTrue6 和 AllAreTrue7 可以编译,但 (1) 丑陋,(2) 效率低下,以及 (3) 不是类型安全的(直到运行时才会发现 'T' 不能转换为 bool)。
有没有办法添加一个通用的约束,上面写着“T 必须实现 operator true”?
编辑
如果有帮助的话,甚至可以说类必须实现到 bool 的隐式转换:
class BatteryIsGood
{
float BatteryVoltage;
public static bool operator true(BatteryIsGood ab) => (bool)ab;
public static bool operator false(BatteryIsGood ab) => !((bool)ab);
public static implicit operator bool(BatteryIsGood big) => ab.BatteryVoltage >= 3;
}
【问题讨论】:
-
可能不是最优雅的,但答案有效吗?
标签: c# boolean operator-overloading