TryParse and out parameters

  • Thread starter Thread starter MC
  • Start date Start date
M

MC

Let's say I want to validate something with TryParse but I don't need
to convert it. In C#, it seems I must provide that out parameter
regardless. In VB, I can pass Nothing. I'd like to be able to do the
same in C# instead of having to declare a "dummy" variable for the out
parameter. In other words, can I do something like this:

if( int.TryParse( str, null ) )
{
// Do something.
}

Thanks,
Craig
 
Good point. With nullable value types, I could write my own
TryParse's. I wonder why Microsoft didn't use a nullable type for the
output parameter. Nullables implicitly accept a non-nullable, so it
seems like a harmless idea. For instance:

int i = 5;
int? nullable = i; // Compiles, no warning.

Craig
 
Back
Top