C # Type Conversions

Source: Internet
Author: User
Tags type casting

Type conversions are essentially type casting, or the conversion of data from one type to another. There are two forms of type casting in C #:

    • implicit type conversions -These transformations are the default, safe conversion of C #, and do not result in data loss. For example, a small integer type is converted to a large integer type, from a derived class to a base class.
    • explicit Type conversions-explicit type conversions, that is, coercion of type conversions. Explicit conversions require a cast operator, and casting can result in data loss.

The following example shows an explicit type conversion:

Namespace typeconversionapplication{    class explicitconversion    {        static void Main (string[] args)        {            double d = 5673.74;            int i;            Casts a double to int            i = (int) d;            Console.WriteLine (i);            Console.readkey ();}}}    

When the above code is compiled and executed, it produces the following results:

5673
C # Type Conversion method

C # provides the following built-in type conversion methods:

Serial Number Method & Description
1 ToBoolean
If possible, convert the type to a Boolean type.
2 ToByte
Converts the type to a byte type.
3 ToChar
If possible, convert the type to a single Unicode character type.
4 ToDateTime
Converts a type (integer or string type) to a date-time structure.
5 ToDecimal
Converts a floating-point or integer type to a decimal type.
6 ToDouble
Converts the type to a double-precision floating-point type.
7 ToInt16
Converts the type to a 16-bit integer type.
8 ToInt32
Converts the type to a 32-bit integer type.
9 ToInt64
Converts the type to a 64-bit integer type.
10 ToSByte
Converts the type to a signed byte type.
11 ToSingle
Converts the type to a small floating-point number type.
12 Tostring
Converts the type to a string type.
13 ToType
Converts the type to the specified type.
14 ToUInt16
Converts the type to a 16-bit unsigned integer type.
15 ToUInt32
Converts the type to a 32-bit unsigned integer type.
16 ToUInt64
Converts the type to a 64-bit unsigned integer type.

The following example converts the type of a different value to a string type:

Namespace Typeconversionapplication{ Class Stringconversion { Static void Main(String[]Args) { IntI= 75; FloatF= 53.005f; DoubleD= 2345.7652; boolB= True; Console.WriteLine(I.Tostring()); Console.writeline (f. Tostring console. Writeline (d. Tostring console. Writeline (b. Tostring console. Readkey} }}    /span>                

When the above code is compiled and executed, it produces the following results:

7553.0052345.7652True

List of Notes
    1. Jennis

      [Email protected]

      Implicit conversions and Explicit conversions

      Implicit conversions: C # Default conversion in Safe mode. The essence is that the small storage capacity data type is automatically converted to a large storage capacity data type, which is converted from derived classes to base classes.

      Instance:

      namespace typeconvertion{class   Class1    {}class class2:class1//Class Class2 is a subclass of class Class1 {}class program{    static void Main (string[] args)    {        int inum = +;        Long lnum = Inum; An implicit conversion was made to convert the data of type int (small data range) to a long (data range Large) data        Class1 c1 = new Class2 ();//This is also an implicit conversion, converting a new CLASS2 instance to its base class Class1 Instance of type C1    }}}

      Explicit conversions: Explicit conversions require the cast operator to be explicitly completed by the user using a predefined function.

      The range size of the conversion type is the opposite of the dependency and implicit conversions. An explicit conversion can result in data errors, or conversion failures, or even compilation success.

      Instance:

      Double Dnum = 100.1;int IFROMD = (int) dnum; The double type is explicitly converted to an int type Class1 C11 = new Class1 (); Class2 C22 = C11 as Class2; Use as for explicit conversion Console.WriteLine (C22 is Class1); Console.WriteLine (C22 is Class2);

      Operation Result:

      Falsefalse

C # Type Conversions

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.