How to safely use System.Object for `bool` in C #?

I am retrieving the bool value from a (not universal, heterogeneous) collection.

as operator can only be used with reference types, so it is not possible to use in as to try the protective shade of bool :

 // This does not work: "The as operator must be used with a reference type ('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 the InvalidCastException if for some reason the value is not logical?

+65
casting c #
Dec 29 '09 at 2:36 p.m.
source share
9 answers

There are two options ... with a bit of amazing performance:

  • Redundant check:

     if (rawValue is bool) { bool x = (bool) rawValue; ... } 
  • Using the nullable type:

     bool? x = rawValue as bool?; if (x != null) { ... // use x.Value } 

The amazing thing 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 will still get value in scope (but not necessarily assigned) after the if .

+113
Dec 29 '09 at 14:43
source share

Like this:

 if (rawValue is bool) { bool value = (bool)rawValue; //Do something } else { //It not a bool } 

Unlike reference types, there is no quick way to apply values ​​to a type without two casts. (Or catch block, which would be worse)

+16
Dec 29 '09 at 14:39
source share
 bool value; if(rawValue is bool) value = (bool)rawValue; else { // something is not right... 
+7
Dec 29 '09 at 14:40
source share

Can you pass it to bool? using the as keyword and check the HasValue property.

+4
Dec 29 '09 at 14:40
source share

You have not determined what should happen if rawValue is not converted to bool. The usual choice is to return false, null or throw an exception. It is also possible to convert the rawValue string representation to a bool type, for example, 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 } 
+4
Dec 29 '09 at 2:51 p.m.
source share

Provided that you really do not need to store a reference to rawValue , here is 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 GetValueOrDefault(T) overload method.

0
Jun 27 '18 at 0:05
source share

I used this check before doing something with the object

 if(myCrazyObject.GetType().Equals(typeof(bool))) { //do smt with it } 
0
Feb 19 '19 at 9:19
source share

If the target is true , only if the raw object has a logical value of true ', then single-line (rawValue as bool?)?? false (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' 
0
Sep 19 '19 at 16:25
source share

You can also try Convert.ToBoolean (rowValue);

-3
Aug 12 '14 at 18:05
source share



All Articles