Return null if the value type object has default value.
-
public static T? GetValueOrNull<t>( this T value ) where T : struct
-
{
-
if( value.Equals( default( T ) ) )
-
return new Nullable<t>();
-
else
-
return value;
-
}
-
-
// Usage
-
int i = default(int); //0
-
int? i = i.GetValueOrNull();
-
Console.Writeline( i.HasValue );
The output is false
( Maybe there is something else doing the same thing. ?! )