title: How do I safely cast a System.Object to a `bool` in C#?
body: <p>I am extracting a <code>bool</code> value from a (non-generic, heterogeneous) collection.</p>
  
  <p>The <code>as</code> operator may only be used with reference types, so it is not possible to do use <code>as</code> to try a safe-cast to <code>bool</code>:</p>
  
  <pre><code>// 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;
  </code></pre>
  
  <p>Is there something similar that can be done to safely cast an object to a value type without possibility of an <code>InvalidCastException</code> if, for whatever reason, the value is not a boolean?</p>