I have many entities that have the IsActive
property. For internal reasons, I need all those fields to be nullable. On the other hand, for every entity I may have to do a double test in tens of places in the application:
(null are treated as true)
if (language.IsActive == null || language.IsActive.value)
If I create a method like
class Language
{
public bool IsActiveLanguage()
{
return language.IsActive == null || language.IsActive.value;
}
}
It still won't hide the property (at least from inside the class) so it's error prone.
I tried to find a way to override, but of course I can't change the return type to plain bool
.
What would you do to avoid redundancy in this case?
You can use the null-coalescing operator, so your example would become:
return language.IsActive ?? true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With