In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how C # implements forced conversion. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The int keyword indicates an integer that is 32-bit, and its .NET Framework type is System.Int32.
(int) indicates the use of explicit casting, which is a type conversion. When we convert int types to long, float, double, or decimal types, we can use implicit conversions, but when we convert long types to int types, we need to use explicit casts, otherwise a compilation error will occur.
Int32.Parse () means to convert a numeric string into a 32-bit signed integer, which belongs to content conversion [1].
Our common method: public static int Parse (string).
If string is empty, an ArgumentNullException exception is thrown
If the string format is not correct, a FormatException exception is thrown
If the value of string is less than MinValue or greater than the number of MaxValue, an OverflowException exception is thrown.
Convert.ToInt32 () can convert values of multiple types (including object reference types) to int types because it has many overloaded versions [2]:
Public static int ToInt32 (object); public static int ToInt32 (bool); public static int ToInt32 (byte); public static int ToInt32 (char); public static int ToInt32 (decimal); public static int ToInt32 (double); public static int ToInt32 (short); public static int ToInt32 (long); public static int ToInt32 (sbyte); public static int ToInt32 (string);.
The applications of (int), Int32.Parse () and Convert.ToInt32 () are given several examples:
Example 1:
Long longType = 100; int intType = longType; / / error, need to use explicit cast int intType = (int) longType; / / correct, using explicit cast
Example 2:
String stringType = "12345"; int intType = (int) stringType; / / error. String type cannot be directly converted to int type int intType = Int32.Parse (stringType); / / correct
Example 3:
Long longType = 100; string stringType = "12345"; object objectType = "54321"; int intType = Convert.ToInt32 (longType); / / correct int intType = Convert.ToInt32 (stringType); / / correct int intType = Convert.ToInt32 (objectType); / / correct
Example 4 [1]:
Double doubleType = Int32.MaxValue + 1.011; int intType = (int) doubleType; / / although the operation is correct, the error result is int intType = Convert.ToInt32 (doubleType) / / throws an OverflowException exception
The differences among int, Int32.Parse () and Convert.ToInt32 () in C# cast:
* used in explicit casting of long type or floating point to int type, but if the converted value is greater than Int32.MaxValue or less than Int32.MinValue, you will get an error result.
The second is used in the string-to-int type conversion process that conforms to the numeric format, and the corresponding exception can be thrown for the wrong string numeric format.
The third can convert multiple types of values to int types, or throw an exception for the wrong value.
No matter what type of numerical conversion is carried out, the accuracy of numerical value must be considered.
Thank you for reading! This is the end of the article on "how to achieve compulsory conversion in C#". I hope the above content can be of some help to you, so that you can 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.