1. Any programming language has relevant data types. C# is no exception, its basic data types include int, short, long, float, double, string, etc. Data types can be converted to each other. However, the conversion process should be noted that small types can be converted into large types, but large types generally cannot be converted into small types. For example, the int type can be converted to the float type, but the float type may not necessarily be converted to the int type. At least this is the case in C and C++, but it has obviously changed in C#. It seems that Microsoft also allows such a form to exist. For example:
double dbl_num=12345678910.456; int k = (int) dbl_num ;//Cases are used here
If the above code is cast to an int type in C and C++, it will definitely make an error, but now it will not make an error in C#. However, the converted value is often overflowing and is not proficient. This requires everyone to pay attention to.
2. Use another method to convert types, such as (), (), etc. to convert them.
likestring str=”100″;
int i=(str);
Note: The type of str removed from quotes must be the same as the type of *.Parse. If 100 is changed to 100.78, it becomes float type, and the error will be reported during runtime: "The format of the input string is incorrect."
3. Use suffix conversion, such as (), which is generally used in other types such as strings or dates.
int i=100; string s=();
4. Use the Convert class to implement conversion, which basically supports conversion between types.
string str=”100″; int i = Convert.ToInt16(str);
Note: The type of str removed from quotes must be the same as the type of Convert.*. If 100 is changed to 100.78, it becomes float type, and the error will be reported during runtime: "The format of the input string is incorrect."