Compare a nullable-int and a string for equality in C#
<p>I often find reason to compare two values or objects in C# - often different types - and I find myself unhappy with the code I write.<br>
The code ends up looking (to me) verbose and unclear and I frequently end up feeling the need to add a comment that says "//check if they're equal".<br>
(I often blame the fact that that there's no nullsafe-dereference operator like I saw in Groovy once.)  </p>

<p>In a recent example, I have two variables:<br>
<code>
string s;<br>
int? i;
</code></p>

<p>Let's say I want to compare them so that I can do something if they're "obviously equal".<br>
I'm defining them as "obviously equal" if either:<br>
a) the int? doesn't contain a number and the string is null, or else...<br>
b) the string is what you'd get if you wrote out the numerical value of the int? in a plain / unformatted fashion.  </p>

<p>[Note that in this case I'm not actually bothered whether the number 1234 is considered equal to the strings "01234" or "1234.00" (or indeed "1234,00", if you're what I consider foreign).<br>
You can have flexibility either way on those as long as "1234" <strong>is</strong> considered equal, and (e.g.) "1233+1" and "1234z" <strong>aren't</strong>.<br>
You can also have flexibility on whether an int? that doesn't contain a number is considered equal to an empty string.]</p>

<p>So, I want some [clear / simple / short - with apologies for the subjectiveness] expression to put in my "if" condition which will perform the check as required above and definitely won't throw an exception.<br>
What are my options?</p>

<p>(If anyone wants to compare their solution to how this might be more easily expressed in other languages, feel free.  It's always useful to know where the greener grass is...)</p>

<p><strong>UPDATE:</strong></p>

<p>My own code boils down to something like...<br>
<code>
if (s == (i.HasValue ? i.Value.ToString() : null))<br>
</code>
which actually doesn't look so bad now that s and i aren't meaningfully named properties on other objects.
[However, I don't really like leaving an "==" comparison in there because (though it works for strings because of string-interning and/or an operator override or something) it's normally checking reference equality, right?   So I'd consider replacing that with string.Equals... but that's even more horrible to read.]</p>

<p>When you also have to check the parent objects (on which the "meaningful properties" exist) for null, the code just gets so long-winded - and that's what was on my mind when I posted the question, I think.
I wanted to see whether everybody else had something I was missing.  </p>

<p>I really want to be able to simply check for the positive condition and have everything else evaluate to false (without throwing a visible exception).  So...</p>

<p><code>
    if (      (someObject.s == null &amp;&amp; someOther.i == null)<br>
        ||<br>
        (int.Parse(someObject.s) == someOther.i)  )  </p>

<p></code>
or similar would be <em>very</em> "happy" if all possible (null-ref / parse) exceptions could be silently coerced to false.<br>
I guess I'm just getting too lazy in my old age....</p>
