Nullable ToString()

Posted by StupidDeveloper on Stack Overflow See other posts from Stack Overflow or by StupidDeveloper
Published on 2010-03-15T17:11:53Z Indexed on 2010/03/15 17:19 UTC
Read the original article Hit count: 494

Filed under:
|

I see everywhere constructions like:

int? myVar = null;
string test = myVar.HasValue ? myVar.Value.ToString() : string.Empty;

Why not use simply:

string test = myVar.ToString();

Isn't that exactly the same ? At least Reflector says that:

public override string ToString()
{
  if (!this.HasValue)
  {
    return "";
  }
  return this.value.ToString();
}

So, is that correct (the shorter version) or am I missing something?

© Stack Overflow or respective owner

Related posts about c#

Related posts about nullable