Conversion of C # data types
Description: Type conversions are essentially type casting, or the conversion of data from one type to another. In C #, type casting has two forms of implicit type conversion and explicit type conversion
One, implicit type conversion
Implicit type conversions---the default, safe conversion of C # without causing data loss. For example, a small integer type is converted to a large integer type, from a derived class to a base class.
Two, explicit type conversion
An explicit type conversion---that is forced type conversion. Explicit conversions require a cast operator, and casting can result in data loss.
namespace TypeConversionApplication{ class ExplicitConversion { static void Main(string[] args) { double d = 5673.74; int i; // 强制转换 double 为 int i = (int)d; Console.WriteLine(i); Console.ReadKey(); } }}
Three, C # method type conversion 3.1, C # built-in suffix type conversion
Note: The above built-in type conversion function in addition to the ToString method is most commonly used, the other basic in development is not how to use, if need to use is covert in the conversion method.
3.2. Convert method and Xxx.parse method
| Convert Method |
function Description |
| convert.toint32 () |
Convert to Integer (int) |
| convert.tochar () |
Convert to character type (char) |
| convert.tostring () |
Convert to String type (string) |
| convert.todatetime () |
|
convert.todouble ()
| |
| conert.tosingle () |
Convert to single-precision floating-point type (float) |
| Parse Method |
function Description |
| Int. Parse (String type variable name) |
Convert to integral type (int) |
| Int. Tryparsestring s, out int result |
Convert to integral type (int) |
| Int32.Parse |
Convert to integral type (int) |
| Double.Parse () |
Convert to double-precision floating-point type (double) |
The difference between 3.2.1, Convert and Parse
In the actual development of the project, we encounter the types that need to be converted are about 3 categories, namely null (NULL), numeric type (including Float,double,int,long, etc.), and string 3. The following are Convert.ToInt32 (), Int. Parse (), Int. TryParse () as an example, describes the differences between the three.
1, Int. Parse (string variable name) This is the way to convert a string of numeric content to an int type
1, the parameter must be a string type, if the string content is not a number or is not shaped, throw formatexception exception;
2. If the string content is empty or null, the ArgumentNullException exception is thrown;
3. Throws a OverflowException exception if the string content represents a number that is outside the range represented by the int type
4, the use of this method to avoid the point is that only the string content can be processed, and the string content can only be expressed in the scope of the INT type
2, Int. TryParse (string s, out int result) also converts the string of numeric content to int type
1, the way than Int. The good thing about parse is that it does not appear to be abnormal. If the conversion succeeds returns True, False is returned if the conversion fails. Obviously, the last parameter is the output value, if the conversion fails, the output value is 0, and if the conversion succeeds, the corresponding value is output.
//例如:1.将字符串“12345”转换成数值数据12345,实现如下:bool=ryParse("12345",int result);bool=true;result=12345; //成功
3, Convert.ToInt32 ()
1. This method not only converts a string to an int type, but also converts a value of another type to an int type. If the variable is an object or string type, when its value is null, it returns 0, without causing a program error.
2, the method for floating point will be rounded.
3, the same way as the cast, can not be used to handle the char type, or return the ASCII code.
Convert.ToInt32("12345"); //将字符串“12345”转换成数值数据12345 Convert.ToInt32(3.14); //将小数型“3.14”转换成整数型数据 !!!!int.Parse("4.5") 异常 Convert.ToInt32(‘true‘); //将字符型‘true‘转换成布尔型数据
3.3, int and string conversion
int a = 15; string s1 = a.ToString(); string s2 = Convert.ToString(a);
There is a problem: what are the differences between the two methods of conversion?
1. The ToString () function can be used to convert numeric data to string types
2, Convert.ToString () is a cast, no matter what type of data can be converted to a string type by using this method.
3, ToString can also be used to complete the conversion of character to string type, in general, the function is the same.
4, Tosring () method does not accept empty parameters, or compile error. The Convert.ToString method supports empty objects.
string s = "18"; int a1 = int.Parse(s); int a2; int.TryParse(s, out a2); int a3 = Convert.ToInt32(s);
1. Convert convert int to string, and can specify the binary of the conversion
2, int is converted to string, you can use the ToString method or convert.tostring ()
3, the string is converted to int, you can use Int. Parse or Int.tryparse method.
Note: Why there is no string. Parse and String.tryparse methods? No need, ToString will do.
Conversion of C # data types