Safely Cast from bool? to bool

Posted on May 4, 2009 by Frederik Vig in C#, Code Snippet

This is a little code snippet for when you need to cast a nullable type to its value type.

if (startPage.ShowMainFeatured.HasValue)
{
   // Safe to cast
   if ((bool) startPage.ShowMainFeatured)
   {
       mainfeatured.Visible = true;
   }
}

Related Posts: