📜  C#|型铸

📅  最后修改于: 2021-05-29 22:19:35             🧑  作者: Mango

先决条件:C#数据类型

当我们将一种数据类型的值分配给另一种数据类型时,就会发生类型转换。如果数据类型兼容,则C#执行自动类型转换。如果不具有可比性,则需要对其进行显式转换,这称为显式类型转换。例如,将int值分配给long变量。

隐式类型转换/自动类型转换

它发生在以下情况:

  • 这两种数据类型是兼容的。
  • 当我们将较小数据类型的值分配给较大数据类型时。

例如,在C#中,数字数据类型彼此兼容,但是不支持从数字类型到char或boolean的自动转换。而且,char和boolean彼此不兼容。转换之前,编译器首先根据下图检查兼容性,然后确定是否正确或存在一些错误。

下表显示了C#支持的隐式转换类型:

Convert from Data Type Convert to Data Type
byte short, int, long, float, double
short int, long, float, double
int long, float, double
long float, double
float double

例子 :

// C# program to demonstrate the
// Implicit Type Conversion
using System;
namespace Casting{
  
class GFG {
  
        // Main Method
        public static void Main(String []args)
        {
            int i = 57; 
              
            // automatic type conversion
            long l = i; 
               
            // automatic type conversion
            float f = l;
              
            // Display Result
            Console.WriteLine("Int value "  +i);
            Console.WriteLine("Long value "  +l);
            Console.WriteLine("Float value "  +f);
        }
}
}
输出:
Int value 57
Long value 57
Float value 57

显式类型转换

类型彼此不兼容时,可能会出现编译错误。例如,将double值分配给int数据类型:

// C# program to illustrate incompatible data 
// type for explicit type conversion
using System;
namespace Casting{
  
class GFG {
  
        // Main Method
        public static void Main(String []args)
        {
            double d = 765.12;
  
            // Incompatible Data Type
            int i = d;
              
            // Display Result    
            Console.WriteLine("Value of i is ", +i);
        }
}
}

错误 :

prog.cs(14,21): error CS0266: Cannot implicitly convert type `double' to `int'.
An explicit conversion exists (are you missing a cast?)

因此,如果我们想将较大数据类型的值分配给较小数据类型,则执行显式类型转换。

  • 这对于无法进行自动转换的不兼容数据类型很有用。
  • 在此,target-type指定将指定值转换为的所需类型。
  • 有时,它可能导致有损转换。

例子 :

// C# program to demonstrate the
// Explicit Type Conversion
using System;
namespace Casting{
  
class GFG {
  
        // Main Method
        public static void Main(String []args)
        {
            double d = 765.12;
  
            // Explicit Type Casting
            int i = (int)d;
  
            // Display Result
            Console.WriteLine("Value of i is " +i);
        }
}
}
输出:
Value of i is 765

解释 :
在此,由于有损转换,i的值变为765,损失值为0.12。

C#提供了用于类型转换的内置方法,如下所示:

Method Description
ToBoolean It will converts a type to Boolean value
ToChar It will converts a type to a character value
ToByte It will converts a value to Byte Value
ToDecimal It will converts a value to Decimal point value
ToDouble It will converts a type to double data type
ToInt16 It will converts a type to 16-bit integer
ToInt32 It will converts a type to 32 bit integer
ToInt64 It will converts a type to 64 bit integer
ToString It will converts a given type to string
ToUInt16 It will converts a type to unsigned 16 bit integer
ToUInt32 It will converts a type to unsigned 32 bit integer
ToUInt64 It will converts a type to unsigned 64 bit integer

例子 :

// C# program to demonstrate the
// Built- In Type Conversion Methods
using System;
namespace Casting{
  
class GFG {
  
        // Main Method
        public static void Main(String []args)
        {
            int i = 12;
            double d = 765.12;
            float f = 56.123F;
              
            // Using Built- In Type Conversion
            // Methods & Displaying Result
            Console.WriteLine(Convert.ToString(f));
            Console.WriteLine(Convert.ToInt32(d));
            Console.WriteLine(Convert.ToUInt32(f));
            Console.WriteLine(Convert.ToDouble(i));
            Console.WriteLine("GeeksforGeeks");
        }
}
}
输出:
56.123
765
56
12
GeeksforGeeks