Home  >  Article  >  Backend Development  >  [C# Tutorial] C# Type Conversion

[C# Tutorial] C# Type Conversion

黄舟
黄舟Original
2016-12-26 13:54:161302browse

C# Type Conversion

Type conversion is fundamentally type casting, or converting data from one type to another. In C#, type casting comes in two forms:

Implicit type conversions - these are the default conversions that C# performs in a safe manner. For example, converting from a small integer type to a large integer type, and from a derived class to a base class.

Explicit type conversions - These conversions are done explicitly by the user using predefined functions. Explicit conversion requires a cast operator.

The following example shows an explicit type conversion:

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();
            
        }
    }
}

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

5673

C# type Conversion Methods

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

Serial Number

Method& Description

1 ToBoolean
Convert the type to Boolean if possible.

2 ToByte
Convert the type to byte type.

3 ToChar
Convert the type to a single Unicode character type if possible.

4 ToDateTime
Convert the type (integer or string type) to a date-time structure.

5 ToDecimal
Convert floating point or integer type to decimal type.

6 ToDouble
Convert the type to double precision floating point type.

7 ToInt16
Convert the type to a 16-bit integer type.

8 ToInt32
Convert the type to a 32-bit integer type.

9 ToInt64
Convert the type to a 64-bit integer type.

10 ToSbyte
Convert the type to a signed byte type.

11 ToSingle
Convert the type to a small floating point number type.

12 ToString
Convert the type to string type.

13 ToType
Convert the type to the specified type.

14 ToUInt16
Convert the type to a 16-bit unsigned integer type.

15 ToUInt32
Convert the type to a 32-bit unsigned integer type.

16 ToUInt64
Convert the type to a 64-bit unsigned integer type.

The following example converts different value types into string types:

namespace TypeConversionApplication
{
    class StringConversion
    {
        static void Main(string[] args)
        {
            int i = 75;
            float f = 53.005f;
            double d = 2345.7652;
            bool b = true;

            Console.WriteLine(i.ToString());
            Console.WriteLine(f.ToString());
            Console.WriteLine(d.ToString());
            Console.WriteLine(b.ToString());
            Console.ReadKey();
            
        }
    }
}

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

75
53.005
2345.7652
True

The above is the content of [c# tutorial] C# type conversion. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn