12

Is there an equivalent of SQL NULLIF function built-in within c#?

Example of what it could look like :

double? result
double denominator = 0;
double Numerator = 100;
result = Numerator / NullIf(denominator, 0);
result = Numerator / denominator.NullIf(0);
result = Numerator / Object.NullIf(denominator, 0);
5
  • You meant for the denominator to be nullable and not the result right?
    – juharr
    Commented Aug 27, 2015 at 15:42
  • I mean to divide by NULL when the value is zero
    – AXMIM
    Commented Aug 27, 2015 at 15:42
  • 2
    just use ternary operator for result. result = denominator == 0 ? (double?)null : Numberator/denomiantor;
    – vittore
    Commented Aug 27, 2015 at 15:43
  • Also consider that double.NaN behaves much like sql null here, and dosn't require nullables et al. 1 / NaN = NaN etc which is I think what you are looking for, you could easily add your own NaNIf extension function.
    – tolanj
    Commented Aug 27, 2015 at 15:50
  • Thanks, for pointing that out. However I will stick to NULL since not all type have NaN. For exemple, integer doesn't have NaN. I prefer to use the same pattern every.
    – AXMIM
    Commented Aug 27, 2015 at 15:56

5 Answers 5

9

No, there is no language feature for this at present.

You can easily get the same result, either with a ternary if:

result = Numerator / (denominator == 0 ? (double?)null : denomiantor);

Or even wrapping it as a generic function, so something like:

Nullable<T> NullIf<T>(T left, T right)
{
    return left == right ? (T?)null : left;
}

which could then be called like:

result = Numerator / NullIf(denominator, 0);
3
  • It works but I really don't like operations on int? , double? etc. imho 5/null == null is confusing.
    – vittore
    Commented Aug 27, 2015 at 15:50
  • 5
    @vittore Seems straight forward to me if you think of null as meaning unknown value. 5 divided by an unknown value is an unknown value.
    – juharr
    Commented Aug 27, 2015 at 16:02
  • 1
    Not worth of it's own answer as yours is acceptable, but it could also be an extension method to be more c#'y although it doesn't exactly mirror the SQL variant ` public static class Extensions { public Nullable<T> NullIf<T>(this T left, T right) => left == right ? (T?)null : left; } ... result = numerator / denominator.NullIf(0)`
    – Will
    Commented Aug 3, 2021 at 17:17
7

Accepted answer give me:

Operator '==' cannot be applied to operands of type 'T' and 'T'

My working example of NULLIF:

public static T? NullIf<T>(T left, T right) where T : struct
{
    return EqualityComparer<T>.Default.Equals(left, right) ? (T?)null : left;
}
4

No but you could create one.

public static Nullable<T> NullIf<T>(T first, T second) where T : struct
{
    if(first == second)
        return new Nullable<T>();
    return new Nullable<T>(first);
}
0
1

There is no. but you can use ternary operator for compact way of writing it:

double? result
double denominator = 0;
double Numerator = 100;
result = denominator == 0 ? (double?)null : Numerator / denominator;

However there is equivalent of IFNULL:

result = x ?? 0; 

equivalent of:

result = x.HasValue? x.Value : 0;
0

Why do we assume this extension method is able to know what is null in our context.

Its the responsibility of the context that calls it. Therefore is a mistake of saying only one value will be considered null as all the response in this thread impliy.

public static T? NullIf<T>(this T value, Func<T,bool> isConsideredNull) 
{
        if(value == null)
        {
           return null;
        }
        return isConsideredNull(value) ? (T?)null : value;
}

We would use it the following way

string test = "NULL";

test.NullIf((x)=> x.Equals("NULL"));
test.NullIf((x)=> x == "NULL");
test.NullIf((x)=> x.Equals("NULL",StringComparison.InvariantCultureIgnoreCase));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.