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));
}

```