3 ms·
C# has kind of an equivalent of that, at least for this purpose, of nullable value types. Thus, you could do: int? result = int.TryParse(s); if(result
by ufmace 13y ago
C# has kind of an equivalent of that, at least for this purpose, of nullable value types. Thus, you could do:
int? result = int.TryParse(s);
if(result.HasValue)
{
Console.WriteLine("Parsed {0}",result.Value);
}
else
{
Console.WriteLine("{0} was not an integer",s);
}
And in C#, you could do the second kinda like:
object result = int.TryParse(s);
if(result is int)
{
Console.WriteLine("Parsed {0}",result);
}
else
{
Console.WriteLine("{0} was not an integer",s);
}
Though there's nowhere simple to make it clear that this TryParse either returns the parsed int, or a string describing why the parse failed. And returning object kinda goes against the spirit of the language, IMHO.