C # Difficulty one by one (9): type conversion

Source: Internet
Author: User
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.