I saw a method return bool?, does anyone know the meaning of it?
I saw a method return bool? , does anyone know the meaning of it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
T?is a C# syntax shortcut toNullable<T>. Sobool?maps toNullable<bool>. So in your case it’s a struct that either isnull, or has aboolvalue.If a
Nullable<T>isnullthat’s a bit different from a reference type being null. It basically is a struct containing the underlying type and a boolean flagHasValue. But both the runtime and the C# language have a bit of magic to pretend that aNullablewithHasValue==falseis really null. But the difference still leaks sometimes.The underlying type is implicitly convertible to the nullable type (
bool->bool?). To get the underlying type from the nullable type, you can either cast explicitly ((bool)myNullableBoolor use theValueproperty (myNullableBool.Value). You should check fornullbefore though, or you’ll get an exception if the nullable value isnull.Nullable<bool>