Geeks With Blogs
Sean's Blog My Development Blog
If you've ever needed to convert a Nullable<T> type to 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 | Back to top


Comments on this post: Converting Nullable Values to Their Default, Non-Nullable Values

# re: Converting Nullable Values to Their Default, Non-Nullable Values
Requesting Gravatar...
Perhaps a better name for this function would be ToValueOrDefault().
Left by Ryan on Sep 14, 2011 1:19 AM

# re: Converting Nullable Values to Their Default, Non-Nullable Values
Requesting Gravatar...
You could also use the framework's value.GetValueOrDefault().
Left by Jesper on Sep 14, 2011 6:15 AM

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

Your comment:
 (will show your gravatar)


Copyright © Sean Fao | Powered by: GeeksWithBlogs.net | Join free