How to Format Nullable object?
<p>This is a case I run into frequently and I was wondering if anyone had a better more concise way of doing this.</p>

<p>Say I have a variable 'x' which is of a type that is nullable. For example: string, int?, or DateTime?</p>

<p>I want to do something with x where I need to format x into a string but I cannot call .ToString() or some other method on x because x might be null.</p>

<p>Yes, I can do something like</p>

<pre><code>if(x == null)
{
 Console.WriteLine("empty");
}
else
{
 Console.WriteLine(x.ToString("G"));
}
</code></pre>

<p>or</p>

<pre><code>Console.WriteLine("{0}", x == null? "empty" : x.ToString("G"));
</code></pre>

<p>but I was looking for something shorter. Maybe nothing exists, but I thought I would ask and start the conversation.</p>

<p>One reason I'm interested in this is because I frequently have a large number of variables where I'm moving data from one class to another or something similar and I need to apply some formatting.</p>

<p>So instead of just x, I've got a, b, c, ... z and I have to check each for null and apply formatting to each and they are of varios types and require different formatting. Something shorter and easier to copy and paste with only minimal changes to each line would be nice.</p>

<p>So does anyone have any clever tricks they use for this?</p>

<p>I also considered the ?? operator but I can't seem to find any good way to apply it. On first glance, it seems like it was designed just for this situation but it doesn't actually help at all.</p>

<p>Its really only useful so long as you don't need to apply any formatting.</p>
