ExtensionMethod.NET Home of 876 C#, Visual Basic, F# and Javascript extension methods


Returns a the value of a Nullable type if it has a value or it will return a default value


public static T DefaultValue<T>(this Nullable<T> value, T defaultValue) where T : struct
    if (value == null || value.HasValue == false)
        return defaultValue;

    return value.Value;


bool? HasVideo = null;
bool value = HasVideo.DefaultValue(false);

Author: Robert Booth

Submitted on: 30 mrt 2009

Language: C#

Type: System.Nullable<T>

Views: 4789