Background
During development, you need to determine whether an input character is a numeric character. If it is a numeric character, it is converted to an integer.
Problem proposal
The char. IsDigit method and int. TryParse method are used during implementation. It is found that char. isDigit determines it as a numeric character and uses int. the TryParse method does not recognize it as a numeric character. Why?
Problem Reproduction
Instance code
using System;namespace QuanJiaoShuZiExp{ class Program { static void Main(string[] args) { string str1 = "4"; string str2 = "4"; Console.WriteLine(char.IsDigit(str1, 0)); Console.WriteLine(char.IsDigit(str2, 0)); int result; Console.WriteLine(int.TryParse(str1, out result)); Console.WriteLine(int.TryParse(str2, out result)); } }}
Code execution result
The result shows that char. isDigit method and int. returns true for all TryParse methods. When a full-width numeric character is input, char. the IsDigit method returns true, whereas int. the TryParse method returns false.
Problem Discussion
Why?
To answer this question, you may have to ask Microsoft, because these two methods are implemented by Microsoft.
No matter what Microsoft thinks, I will talk about this issue from my own perspective.
First, we know that the halfwidth is the standard usage of international input methods. In order to adapt to Chinese Input habits, the Chinese Input Method adds the fullwidth. Secondly, we know that the halfwidth is ASCII characters, and the fullwidth refers to various symbols in the GB2312-80 ("information exchange with Chinese character encoding Character Set-basic set. Since the full-width characters and the half-width characters come from different character sets, the encoding of the full-width numeric characters and the half-width numeric characters may be different (I am not sure about this, because do not know much about GB2312-80 encoding, please kindly enlighten me), So I boldly guess, char. the IsDigit method may convert the input character encoding to make the full-width numeric character equivalent to the half-width numeric character, while the int. the TryParse method does not exist, so the full-width half-width characters are treated separately.
Let's talk about it today. I feel like I still want to talk about it. I don't want to write it too much. I will try again later.
I want to raise a question here. I hope you can give me some advice.