I often have to call the Value property when accessing my Linq to SQL objects to check for null values or I get an exception. Can someone please expain these data types (i.e. decimal?, bool?, etc…) that appear to wrap the primitive types?
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.
They are Generics of type
Nullable<T>, and they do wrap primitive types.Why they invented the short form int? is Nullable seems to be down to the standard confusion between succinct and terse C based language developers struggle with.
total.HasValuewill return false, it won’t blow up with a null referencebut
total.Value.ToString();will throw an exception, because theValueproperty of total is null.The
Valueand HasValue properties are read only.means total.Value will return 10.0 and total.HasValue will return true.
It’s a really nice generic, especially for database types, still don’t get the short form though…