Generic type that extends bool

How can I make my generic type "extend" boolean?
c#
class MyClass<T> where T : bool {
}
c#
class MyClass<T> where T : bool {
}
4 Replies
Angius
Angius2y ago
That generic parameter would be quite useless, wouldn't it? System.Boolean is a sealed record, so you can't extend it The only possible route is composition, basically
class MyClass
{
private bool _value;
}
class MyClass
{
private bool _value;
}
and maybe explicit cast operators And other overloads that would make it compatible with bool
oke
oke2y ago
im pretty sure that all base types are sealed to avoid program obfuscation (complications) things like strings, bools, ints, etc
wasabi
wasabi2y ago
They're sealed because they're not classes. They're value types in the CIL, and you cannot inherit from value types. But they're also primitives.
JerryJackson
JerryJackson2y ago
why do you want to do this

Did you find this page helpful?