Advertisement
ivandrofly

How to: Safely Cast from bool? to bool (C# Programming Guide

Sep 1st, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.35 KB | None | 0 0
  1. bool? test = null;
  2. // Other code that may or may not
  3. // give a value to test.
  4. if(!test.HasValue) //check for a value
  5. {
  6.     // Assume that IsInitialized
  7.     // returns either true or false.
  8.     test = IsInitialized();
  9. }
  10. if((bool)test) //now this cast is safe
  11. {
  12.    // Do something.
  13. }
  14.  
  15. // Source: https://msdn.microsoft.com/en-us/library/bb384091.aspx
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement