Explicit conversions and implicit conversions
Conversions between types can be divided into implicit conversions and explicit conversions, such as int types that can be converted directly to a long type.
/* Implicit conversion */int intnum = 23; Long longNum1 = Intnum;
Explicit conversions can also be performed;
/* Explicit conversion */int intnum = 23; Long longNum2 = (long) intnum;
Parse
Each value type contains the parse () law, which converts the string to the corresponding numeric type]
/*parse Convert */String text = "3.1415926E1"; float PI = float. Parse (text);
Convert
System.Convert belongs to a class in the. NET Framework and is a non-inheriting class that defines a prototype in MSDN as
The public sealed class Convertsystem.convert only supports a predefined number of types, and it supports types that include all value types plus the base data type of type string and the object type, where the base data type is. Net The data type of the framework.
/*convert conversion */bool Istrue=false; String strnum = Convert.ToString (isTrue);
TryParse ()
TryParse belongs to the C # static method, and all basic data types contain this method. It returns the bool type, indicating whether the conversion succeeded, which is often paired with out to transform the data, and if the conversion fails, it returns false and does not throw an exception.
/*tryparse convert */System.Console.Write ("Enter the value to convert:"); string input = System.Console.ReadLine ();//input int result;//conversion result if (int. TryParse (input, out result)) {System.Console.WriteLine ("conversion result: {0}", result);} else {System.Console.WriteLine ("conversion failed") ; }
ToString ()
All types support the ToString () method, which returns a string representation of the value of the type object only if the type explicitly implements ToString (), and the other type, ToString (), is simply the name of the returned data type.
Object o = new Object (); Console.WriteLine (O.tostring ()); int age = 23; Console.WriteLine (age. ToString ()); bool Ismale = true; Console.WriteLine (Ismale.tostring ());
The above is the C # difficulty one by one (9): type conversion content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!