How do I safely cast a System.Object to a `bool` in C#?
I am extracting a bool
value from a (non-generic, heterogeneous) collection.
The as
operator may only be used with reference types, so it is not possible to do use as
to try a safe-cast to bool
:
// This does not work: "The as operator must be used with a reference typ开发者_JS百科e ('bool' is a value type)"
object rawValue = map.GetValue(key);
bool value = rawValue as bool;
Is there something similar that can be done to safely cast an object to a value type without possibility of an InvalidCastException
if, for whatever reason, the value is not a boolean?
There are two options... with slightly surprising performance:
Redundant checking:
if (rawValue is bool) { bool x = (bool) rawValue; ... }
Using a nullable type:
bool? x = rawValue as bool?; if (x != null) { ... // use x.Value }
The surprising part is that the performance of the second form is much worse than the first.
In C# 7, you can use pattern matching for this:
if (rawValue is bool value)
{
// Use value here
}
Note that you still end up with value
in scope (but not definitely assigned) after the if
statement.
Like this:
if (rawValue is bool) {
bool value = (bool)rawValue;
//Do something
} else {
//It's not a bool
}
Unlike reference types, there's no fast way to try to cast to a value type without two casts. (Or a catch block, which would be worse)
bool value;
if(rawValue is bool)
value = (bool)rawValue;
else {
// something is not right...
With C# 9 you can also simplify to:
if(rawValue is true)
{
//do stuff
}
You haven't defined what you want to have happen if rawValue is not convertible to bool. Common choices are to return false, null, or throw an exception. There's also the possibility of the string representation of rawValue to be convertible to a bool, such as Yes/No, True/False, 1/0, etc.
I would use bool.TryParse to do the conversion. This will succeed if rawValue is a bool or its string value is "True" or "False".
bool result;
if (!bool.TryParse(rawValue as string, out result))
{
// you need to decide what to do in this case
}
You can cast it to a bool?
with the as
keyword and check the HasValue
property.
Providing you don't actually need to keep a reference to the rawValue
, here's a one-liner using the GetValueOrDefault()
method of the Nullable<T>
structure:
bool value = (map.GetValue(key) as bool?).GetValueOrDefault();
You can also specify a default value using the method overload GetValueOrDefault(T)
.
I used this check before doing something with object
if(myCrazyObject.GetType().Equals(typeof(bool)))
{
//do smt with it
}
If the goal is to have true only if the raw object is boolean 'true' then one-liner (rawValue as bool?)?? false
will do:
object rawValue=null
(rawValue as bool?)?? false
false
rawValue="some string"
(rawValue as bool?)?? false
false
rawValue=true
(rawValue as bool?)?? false
true
rawValue="true"
(rawValue as bool?)?? false
false
rawValue=false
(rawValue as bool?)?? false
false
rawValue=""
(rawValue as bool?)?? false
false
rawValue=1
(rawValue as bool?)?? false
false
rawValue=new Dictionary<string,string>()
(rawValue as bool?)?? false
false`
bool value = (rawValue is null) ? false : (bool)rawValue.value;
if rawValue is null
then value will be false
, otherwise value will receive the correct boolean value.
You can also try Convert.ToBoolean(rowValue);
精彩评论