Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of C # Convert.ToInt32

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of C# Convert.ToInt32 for you. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

For example, if you have a string type 3, do you want to convert it to int type with (int) 3 or C # Convert.ToInt32 (3); or both, why?

Answer: both of them are converted to integers, but their lengths are different. The int is 16-bit, while the one below is 32-bit

First of all, I would like to point out that in C #, int is actually System.Int32, that is, both 32-bit.

Second, (int) and C # Convert.ToInt32 are two different concepts, the former is type conversion, while the latter is content conversion, they are not always equivalent. We know very well that C # provides type checking. You can't cast a string into int, so implicit conversion is even more impossible. For example, the following code won't work:

String text = "1412"; int id = (int) text

Because string and int are two completely different and incompatible types. Speaking of which, you might ask what is compatible? In fact, it is only numeric types that can use (int) for strong type conversion, such as long, short, double, etc., but you need to consider precision when doing this conversion.

However, we are well aware that text in the above code actually stores a value, and we want to extract this value and store it in int for later calculation, so you need to convert the content. Content conversion is also called content interpretation, and we can achieve our goal by slightly modifying the above code:

String text = "1412"; int id = Convert.ToInt32 (text)

In addition, you can also use Int32.Parse and Int32.TryParse to explain.

In addition, you find that there are many overloaded versions of C # Convert.ToInt32, such as C # Convert.ToInt32 (doublevalue) When we use this version to convert a double to int, ToInt32 will check whether the converted value can be expressed in int, that is, whether "out of bounds" will occur. If so, OverflowException will be thrown, otherwise it will be converted for you, but cast with (int). If the converted value is greater than Int32.MaxValue, you will get an error result, such as the following code:

Double d = Int32.MaxValue + 0.1412; int I = (int) d

However, no matter what numerical conversion you do, the problem of accuracy must be considered.

This is the end of the sample analysis on C # Convert.ToInt32. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report