Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What's the difference between conversion? ToInt32 () and (int) (turn)?
What's the difference between conversion? ToInt32 () and (int) (turn)?
Both are converted to integers, but the lengths are different. Int is 16 bit, and the lower bit is 32 bit.

First of all, I want to point out that in C#, int is actually System. Int32, which means all 32 bits.

Secondly, (int) and Convert. ToInt32 is two different concepts, the former is type conversion and the latter is content conversion, which are not always equivalent. We know that C# provides type checking. You can't forcibly convert a string to int, let alone implicitly convert it. For example, the following code doesn't work:

string text = " 14 12 ";

int id =(int)text;

But we know very well that the text in the above code actually stores a numerical value. We want to extract this numerical value and store it in the form of int for future operation, so you need to convert the content. Content conversion, also called content interpretation, can be achieved by slightly modifying the above code:

string text = " 14 12 ";

int id = Convert。 ToInt32 (text);

In addition, you can also use Int32. Parse and Int32. Let me explain.

In addition, you find that Convert has many overloaded versions. ToInt32, such as Convert. ToInt32 (binary); When we use this version to convert double Into int, ToInt32 will check whether the converted value can be represented by int, that is, whether it will be "out of bounds". If it is, it will throw an OverflowException, otherwise it will be converted for you, but use (int) for forced conversion. If the converted value is greater than Int32. MaxValue, then you will get a wrong result, such as the following code: