C# equivalent of the IsNull() function in SQL Server
It's called the null coalescing (??
) operator:
myNewValue = myValue ?? new MyValue();
Sadly, there's no equivalent to the null coalescing operator that works with DBNull; for that, you need to use the ternary operator:
newValue = (oldValue is DBNull) ? null : oldValue;
public static T isNull<T>(this T v1, T defaultValue)
{
return v1 == null ? defaultValue : v1;
}
myValue.isNull(new MyValue())