Converts a single character to an integer.

Source: Internet
Author: User

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:


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.