How to

create null-able value type

Published: 26. September 2011 | Updated: 26. October 2011
License: Microsoft Public License (MS-PL)
Categories: Framework » Types
Tags: C#
Was this snippet helpful for you? YESYES / NONO

Declaration

int? number = null;

// or

Nullable<int> number = null;

Check for null

if(number.HasValue) ...

// or

if(number != null) ...

Get value

int nonNullable = (int)number;

// or

int nonNullable = number.Value;

// or

int nonNullable = number.GetValueOrDefault();
Send us feedback about this snippet »



Related Snippets: