Automatic type conversion
Implicit Type Conversions - These conversions are C#'s default conversions in a safe manner and do not cause data loss. For example, convert from a small integer type to a large integer type, and from a derived class to a base class.
Conversion rules
From types with small storage range to types with large storage range.
The specific rules for integers are:
byte→short(char)→int→long→float→double
In other words, variables of type byte can be automatically converted to short type, sample code:
byte b = 10; short sh = b;
You can jump during type conversion. Sample code:
byte b1 = 100; int n = b1;
Force type conversion
Explicit type conversion - Explicit type conversion, i.e. cast type conversion. Explicit conversion requires a cast operator, and casting can cause data loss.
Conversion rules
From types with large storage range to types with small storage range.
The specific rules are:
double→float→long→int→short(char)→byte
For example:
double d = 5673.74; int i; i = (int)d;
.ToInt32() 2. ()
Convert.ToInt32()
Then you can convert values of multiple types (including object reference type) to int type because it has many overloaded versions:
- 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);
()
Indicates converting a string containing a number into a 32-bit signed integer, which belongs to content conversion
- If string is empty, an ArgumentNullException exception is thrown;
- If the string format is incorrect, a FormatException exception is thrown;
It can be seen thatConvert.ToInt32()
The function is the most powerful, it puts()
Functions include, which is also a special case where () is Convert.ToInt32().
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links