2

When using the char datatype is there any reason one should use int.TryParse

int.TryParse(inputChar.ToString(), NumberStyles.Integer, 
                             CultureInfo.InvariantCulture, out curNum)

vs.

inputChar - '0'

And checking if the result is between 0-9?

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • 2
    Can you please clarify the question - as it reads, it's very difficult to understand what's being asked – Charleh Feb 26 '14 at 14:23
  • Well, what about when you don't have a `char`, but a string containing the value `"192384"`? How would you subtract `0`? Also, while that may work in "our" cultural environment, the thing about different cultures and character sets is that the character code for `0` may not in all cases be smaller than the character code of every other number. – Thorsten Dittmar Feb 26 '14 at 14:26
  • Oh I see. You're talking about the case where it is only a single `char` not a string. – Martin Smith Feb 26 '14 at 14:27
  • 1
    What's unclear about the question? The OP asks why he should use a standard library function to parse an integer rather than doing his own conversion the usual hacky way. It might be considered off-topic for SO, but what's unclear about it? – Luaan Feb 26 '14 at 14:30

3 Answers3

1

If you want to check if a char is a digit you should use Char.IsDigit:

if (Char.IsDigit(inputChar))
{ 
    // ...
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Looks like that includes additional unicode characters outside of Ascii `0-9` so I suppose it depends what the OP requires this for. – Martin Smith Feb 26 '14 at 14:46
  • @MartinSmith: normally it just checks `c >= '0' && c <= '9'` with [latin-1](http://msdn.microsoft.com/en-us/library/ms537495(v=vs.85).aspx). – Tim Schmelter Feb 26 '14 at 14:52
  • `Char.IsDigit('۵')` is true for example. Haven't tested if that is the same semantics as the `int.parse` shown in the question. – Martin Smith Feb 26 '14 at 14:54
  • @MartinSmith: i assume if you need to fiddle around with such characters you also need it to be a digit/number. So either this doesn't matter because you aren't using them or it would be incorrect to check only `c >= '0' && c <= '9'`. – Tim Schmelter Feb 26 '14 at 14:58
  • @MartinSmith: by the way, since i've looked at the regex approach you have linked in the other answer, interestingly it returns the same list of 310 characters as `Char.IsDigit`. – Tim Schmelter Feb 26 '14 at 15:55
  • And it appears these are not the same as are accepted by `[Try]Parse` unless I miss some option? e.g. `int.Parse("3", NumberStyles.Any, CultureInfo.InvariantCulture)` returns an error rather than 3. – Martin Smith Feb 26 '14 at 16:23
0

Well, two reasons why I would always use TryParse

  1. Using a well-tested library function is always better than re-inventing the wheel.
  2. The world outside the US doesn't speak "ASCII" - so there might be cases when the character code for 0 is not the smallest for a digit. In that case '9' - '0' != 9;. This is a might be. And because I'm too lazy to google this I'm on the safe side using TryParse :-)
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 1
    @MartinSmith I wonder: How can it be the accepted answer to the question you quoted has been upvoted 1068 times, but the person who wrote the answer only has 6371 reputation? – Thorsten Dittmar Feb 26 '14 at 14:54
  • 1
    Rep cap. Probably many of those 1000 votes were all on the same few days. – Martin Smith Feb 26 '14 at 14:56
  • Oh, see? I didn't know that. That's a shame, really. Assume I answer 5 questions today and each answer is upvoted 10 times today, I still get only 200 points... Well, most probably this has been discussed on meta extensively ;-) – Thorsten Dittmar Feb 26 '14 at 15:00
-1

That's only about code clarity. int.TryParse clearly states its intent - I want to parse the string as number, if possible. It's relatively fast and safe.

If you find yourself getting stuck on TryParses, you can always write your own parsing. In some cases, it can save significant amount of time. For example, I've done such an implementation when parsing DBFs, which otherwise induced a lot of overhead from parsing bytes to strings, and strings to ints. Directly converting from the stream to int saved a lot of allocations and time.

After all, if you don't want to use built-in methods, why use .NET at all? Why not write everything in machine code? :))

Luaan
  • 62,244
  • 7
  • 97
  • 116