Home  >  Article  >  Web Front-end  >  How to convert a character to an integer in C#

How to convert a character to an integer in C#

亚连
亚连Original
2018-06-21 12:02:313819browse

The editor below will share with you a C# implementation of converting a character into an integer. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

Convert a character to an integer as required by the title. There are several methods to implement this function

Method 1: Convert.ToInt32(string);

Run code:

##Method 2: int.Parse(object):

Running result:

This character happens to be a string of numbers. There is no problem using int.Parse(), but if this character is non-number What about strings?

When running, an exception occurred:

Method three: Int.TryParse( obj)

This method can well solve the exception problem of the method. If an exception is thrown, it returns 0:

Run results:

After the above various methods or operation conditions, do we have a comprehensive method? That is to say, no matter what object is converted, it can run normally! Below Insus.NET tries to write an extension method:

Use this extension method in the program:

Running results :

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to use the scroll bar position to determine

How to dynamically set the placeholder prompt text using the WeChat applet

About ES6/JavaScript usage skills (detailed tutorial)

How to crawl website images in nodejs

The above is the detailed content of How to convert a character to an integer in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn