C# 中的类型转换定义为我们将任何数据类型分配给另一种数据类型,然后将其称为“类型转换”。任何编程语言的较低数据类型值都可以自动转换为较高数据类型值。在这种情况下,不会丢失数据,而在将较高数据类型值转换为较低数据类型值的情况下,可能会丢失数据。低层数据类型到高层数据类型可以由 C# 编译器自动完成,但是高层数据类型到低层数据的类型转换,我们必须需要显式类型转换。这就是所谓的“显式强制转换”。
让我们举个例子,将 long 值转换为 int 值是显式类型转换。
打个小比方你就明白了,有2个水瓶,一个是1升,另一个是2升。我们可以轻松地将1升水倒入2升水瓶中而不会溢出。同样的道理,如果我们尝试将 2 升水瓶中的水倒入 1 升水中,那么如果 2 升水瓶中的水超过 1 升,水可能会溢出。因此,在本例中,1 升水瓶是较低的数据类型,2 升水瓶是较高的数据类型。即使有机会溢出水,我们仍然想将2升水倒入1升水瓶中,我们可以倒这样客户必须接受这样做。同样,开发人员有明确的想法,即使我们尝试将较高的数据类型转换为较低的数据类型,也可能会丢失数据,所以他必须接受它。
C# 中有 2 种类型的转换。
字节->短->整数->长->浮点->双
代码:
Bigger_dataType variableName=smaller_dataType_Value;
较大数据类型到较小数据类型的转换被称为“显式类型转换”。 C# 编译器不会自动完成此操作。可能会丢失数据。这必须由开发人员明确完成。
字节->短、整型、长、浮点、双精度
短->int、long、float、double
int->long、float、double
长->浮动,双
浮动->双
代码:
Smaller_dataType variableName=(Smaller_dataType)Bigger_dataType_Value;注意: 转换仅适用于兼容的数据类型,意味着仅进行数字转换的数字,而不是具有数字的字符串,反之亦然,等等。我这样做可能会发生不支持的异常。
下面是一些例子:
隐式类型转换
代码:
//including System package in C# using System; //Creating class public class ImplicitTypeCasting { // main method for application to run public static void Main(String []args) { //variable declaration and initialization int intType = 200; // Implicit int to long casting long longType = intType; // Implicit long to float casting float floatType = longType; // Printing output of implicit conversion variables Console.WriteLine("INT value after Implicit conversion: " +intType); Console.WriteLine("LONG value after Implicit conversion:" +longType); Console.WriteLine("FLOAT value after Implicit conversion: " +floatType); } }
输出:
尝试从更大的类型到更低的类型隐式类型转换
代码:
//including System package in C# using System; //Creating class public class ImplicitCastingBiggerToSmaller { // main method for application to run public static void Main(String []args) { //variable declaration and initialization int intType = 200; // Trying to convert int to byte Implicitly but there is compile time error byte byteType = intType; // Trying to convert int to short Implicitly but there is compile time error short shortType = intType; // Printing output of implicit conversion variables Console.WriteLine("INT value after Implicit conversion: " +intType); Console.WriteLine("BYTE value after Implicit conversion:" +byteType); Console.WriteLine("SHORT value after Implicit conversion: " +shortType); } }
输出:
显式类型转换
代码:
//including System package in C# using System; //Creating class public class ExplicitCastingBiggerToSmaller { // main method for application to run public static void Main(String []args) { //variable declaration and initialization int intType = 9999999; int intType1=120; // Trying to convert int to byte explicitly byte byteType = (byte)intType; byte byteType1 = (byte)intType1; // Trying to convert int to short explicitly short shortType = (short)intType; short shortType1 = (short)intType1; // Printing output of explicit conversion variables //Given int range is not in byte and short range so there must be loss of data result into incorrect output Console.WriteLine("BYTE value after Explicit conversion: " +byteType); Console.WriteLine("SHORT value after Explicit conversion: " +shortType); Console.WriteLine("\n"); // Printing output of explicit conversion variables //Given int range is in byte and short range so there no data loss Console.WriteLine("BYTE value after Explicit conversion: " +byteType1); Console.WriteLine("SHORT value after Explicit conversion: " +shortType1); } }<strong> </strong>
输出:
使用预定义方法进行显式类型转换
代码:
//including System package in C# using System; //Creating class public class ExplicitCastingBiggerToSmaller { // main method for application to run public static void Main(String []args) { //variable declaration and initialization int intType = 9999999; double doubleType=122.23; float floatType=222.222f; // Printing output of explicit convertion variables //By using C# predefined method for type casting Console.WriteLine("INT to STRING type value with predefined method convertion: "+Convert.ToString(intType)); Console.WriteLine("DOUBLE to INT type value with predefined method convertion: "+Convert.ToInt32(doubleType)); Console.WriteLine("FLOAT to INT type value with predefined method convertion: "+Convert.ToUInt32(floatType)); } }
输出:
C# 有两种类型的类型转换,第一种st 一种是隐式类型转换,第二种是显式类型转换。隐式类型转换由编译器自动完成,但显式类型转换开发人员必须执行,因为在这种情况下可能会丢失数据。
以上是C# 中的类型转换的详细内容。更多信息请关注PHP中文网其他相关文章!