Sean's Blog

My Development Blog

  Home  |   Contact  |   Syndication    |   Login
  18 Posts | 0 Stories | 99 Comments | 0 Trackbacks

News

Twitter












Archives

.NET Development

If you've ever needed to convert a Nullable<T> type to a its default, non-nullable value when its nullable value is null, here's a handy extension method to help you out.

public static T NullableToValue<T>(this T? value) where T : struct { return value ?? default(T); }
posted on Tuesday, September 13, 2011 2:45 PM

Feedback

# re: Converting Nullable Values to Their Default, Non-Nullable Values 9/14/2011 1:19 AM Ryan
Perhaps a better name for this function would be ToValueOrDefault().

# re: Converting Nullable Values to Their Default, Non-Nullable Values 9/14/2011 6:15 AM Jesper
You could also use the framework's value.GetValueOrDefault().

# re: Converting Nullable Values to Their Default, Non-Nullable Values 9/14/2011 10:52 AM Sean
Ah...I wasn't aware that method already existed. Thanks for the info.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: