Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# IsNullOrZero

Tags:

c#

This pattern comes up very frequently in my code:

x= x== 0? 1: x;
//or
x= x==null? 1: x;

However it happens that sometimes x is a long expression and I'd have to use intermediate variables. That's just useless boilerplate code. I can cook up a method and call it instead:

Util.IfNullOrZero(x, 1);

But that's just ugly. What is the best way of expressing the pattern? In ruby there is such syntax for when x is nil which gets rid of redundant x's:

x||= 1

I could extend object in a manner

public static class wtf
{
    public static T Default<T>(this object o, T d)
    {
        return o == null ? d : new object[] { o }.Cast<T>().First();
    }
}

And then do

object param= null;
int x= param.Default(1);

But that's a bit expensive.

In short how to best make C# do x||= 1 like in ruby?

Update

This is what I cooked up. I'm currently looking for a faster way of using the Template parameter to convert object to T.

public static class MyExtensions
{
    public static T d<T>(this object o, T d)
    {
        return o == null || o.Equals(default(T)) ? d : new object[] { o }.Cast<T>().First();
    }
}

In fact the code does three things at once: Casts to default type, checks for default value and also checks for null.

Update 2

return o == null || o.Equals(default(T)) ? d : (T)o; // much simpler and faster

I still think it is a commonality which needs to be included in core language.

Update 3 This is what I finally wrote, taking into account DataTable DBNull types.

public static T d<T>(this object o, T d)
{
    return o == null || (o is System.DBNull) || o.Equals(default(T)) ? d : (T)Convert.ChangeType(o, typeof(T));
}
like image 685
nurettin Avatar asked Mar 20 '26 20:03

nurettin


2 Answers

For handling the "==null" case, the null coalesce operator does the trick.

y = x ?? z;

means

if (x == null)
    y = z;
else
    y = x;

I'm not aware of something that check for both zero and null, writing a method to perform this task might be the best solution. Here it goes:

public static T IsNullOrZero<T>(this T variable, T defaultValue)
        {
            // defaultValue can't be null, doesn't make sense
            if (defaultValue == null) throw new ArgumentException("default value can't be null", "defaultValue");
            if (variable == null || variable.Equals(default(T))) 
                return defaultValue;
            return variable;
        }

Usage:

x = x.IsNullOrZero(y);

Note: this in fact works on non-numbers too (name might be misleading if dealing with non-numbers... maybe something along the lines of IsNullOrDefault might be a better name).

like image 108
Alex Avatar answered Mar 24 '26 22:03

Alex


You can check like

public static bool IsNullOrValue(this int? value, int valueToCheck)
 { 
    return (value??valueToCheck) == valueToCheck; 
 } 

more on here

like image 33
Sai Kalyan Kumar Akshinthala Avatar answered Mar 24 '26 22:03

Sai Kalyan Kumar Akshinthala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!