Converts a single character to an integer.
Converts a character to an integer as required by the title. There are several ways to implement this function
Method 1: Convert. ToInt32 (string );
Run the Code:
Method 2: int. Parse (object ):
Running result:
This character is exactly a string of numbers. There is no problem with int. Parse (), but what if it is a non-numeric string?
An exception occurred during running:
Method 3: Int. TryParse (obj)
This method can well solve the exception problem of the method. For example, when an exception is thrown, it returns 0:
Running result:
After the above methods or running conditions, do we have a comprehensive method? That is, it can run normally regardless of the conversion of any object!
The following Insus. NET attempts to write an extension method:
Use this extension method in the program:
Running result: