bool? isTall = null; if (isTall.HasValue) { Console.WriteLine("The person is either tall or short."); } else { Console.WriteLine("We don't know if the person is tall or short."); }
bool? isActive = true; bool isActiveValue = isActive.GetValueOrDefault(); Console.WriteLine($"The user is {(isActiveValue ? "active" : "inactive")}.");In this example, we declare a variable called `isActive` which is of type bool?. We assign a value of true to it, indicating that the user is active. We then use the `GetValueOrDefault()` method to get the value of the `isActive` variable. If `isActive` has a value, it will return that value. If it doesn't have a value, it will return the default value for the bool type, which is false. We then use a ternary operator to print a message saying that the user is either active or inactive, depending on the value of `isActive`. Package library: .NET Framework.