编辑
2025-02-03
C# 应用
00
请注意,本文编写于 92 天前,最后修改于 92 天前,其中某些信息可能已经过时。

目录

1. 什么是类型转换?
1.1 隐式转换
1.2 显式转换
2. 数值类型转换
3. 使用 as 操作符
4. 使用 is 操作符
5. 自定义类型转换
6. 安全类型转换
7. 高级技巧
7.1 接口转换
7.2 方法转换
7.3 泛型方法中的类型转换
7.4 使用反射进行类型转换
结论

在C#编程中,类型转换是一个非常重要且常用的概念。它允许我们将一种数据类型转换为另一种数据类型,就像变魔术一样令人兴奋。本文将深入探讨C#中的类型转换、隐式操作符等关键概念,并通过多个示例帮助你更好地理解和应用这些知识。

1. 什么是类型转换?

类型转换是将一种数据类型转换为另一种数据类型的过程。在C#中,这个过程可以是隐式的(自动进行),也可以是显式的(需要程序员明确指定)。

1.1 隐式转换

当较小的数据类型转换为较大的数据类型时,通常可以进行隐式转换。例如:

C#
int x = 10; double y = x; // 隐式转换,不需要显式指定 Console.WriteLine($"x: {x}, y: {y}");

输出:

image.png

1.2 显式转换

当较大的数据类型转换为较小的数据类型时,通常需要显式转换。这是为了防止可能的数据丢失。例如:

C#
double d = 9.78; int i = (int)d; // 显式转换,需要使用转换操作符 Console.WriteLine($"d: {d}, i: {i}");

输出:

image.png

2. 数值类型转换

在C#中,数值类型之间的转换是最常见的。让我们看几个例子:

C#
// 整数到浮点数 int a = 5; float b = a; // 隐式转换 double c = a; // 隐式转换 // 浮点数到整数 double d = 9.78; int e = (int)d; // 显式转换,小数部分被截断 // 大整数到小整数 long f = 1000; int g = (int)f; // 显式转换 // 小整数到字节 int h = 168; byte i = (byte)h; // 显式转换 Console.WriteLine($"a: {a}, b: {b}, c: {c}"); Console.WriteLine($"d: {d}, e: {e}"); Console.WriteLine($"f: {f}, g: {g}"); Console.WriteLine($"h: {h}, i: {i}");

输出:

image.png

3. 使用 as 操作符

as 操作符尝试将一个对象转换为指定的类型,如果转换失败,则返回 null。这在处理引用类型时特别有用:

C#
object obj1 = "Hello, World!"; string str = obj1 as string; object obj2 = 123; string num = obj2 as string; Console.WriteLine($"str: {str ?? "null"}"); Console.WriteLine($"num: {num ?? "null"}");

4. 使用 is 操作符

is 操作符用于检查对象是否与给定类型兼容:

C#
object obj1 = "Hello, World!"; object obj2 = 123; if (obj1 is string) { Console.WriteLine("obj1 is a string"); } if (obj2 is int) { Console.WriteLine("obj2 is an integer"); }

5. 自定义类型转换

在C#中,我们可以为自定义类型定义隐式和显式转换操作符。这使得类型之间的转换更加灵活。

C#
public class Celsius { public double Temperature { get; } public Celsius(double temperature) { Temperature = temperature; } // 定义从Celsius到Fahrenheit的隐式转换 public static implicit operator Fahrenheit(Celsius c) { return new Fahrenheit(c.Temperature * 9 / 5 + 32); } } public class Fahrenheit { public double Temperature { get; } public Fahrenheit(double temperature) { Temperature = temperature; } // 定义从Fahrenheit到Celsius的显式转换 public static explicit operator Celsius(Fahrenheit f) { return new Celsius((f.Temperature - 32) * 5 / 9); } }
C#
// 使用示例 Celsius c = new Celsius(100); Fahrenheit f = c; // 隐式转换 Celsius c2 = (Celsius)f; // 显式转换 Console.WriteLine($"100°C = {f.Temperature}°F"); Console.WriteLine($"{f.Temperature}°F = {c2.Temperature}°C");

6. 安全类型转换

在进行类型转换时,总有可能转换失败。为了避免异常,我们可以使用 try-catch 块或者结合使用 isas 操作符:

C#
object obj = "123"; // 使用 try-catch try { int num = (int)obj; // 这会抛出异常 } catch (InvalidCastException) { Console.WriteLine("Cannot cast string to int directly"); } // 使用 is 和 as if (obj is string) { string str = obj as string; if (int.TryParse(str, out int result)) { Console.WriteLine($"Successfully converted to int: {result}"); } else { Console.WriteLine("String could not be parsed to int"); } }

7. 高级技巧

7.1 接口转换

接口转换可以让你在不同的实现之间进行灵活的类型转换。例如:

C#
IFormatProvider formatProvider = new CultureInfo("en-US"); DateTimeFormatInfo dtfi = formatProvider.GetFormat(typeof(DateTimeFormatInfo)) as DateTimeFormatInfo; if (dtfi != null) { Console.WriteLine("Successfully converted to DateTimeFormatInfo"); } else { Console.WriteLine("Conversion failed"); }

7.2 方法转换

方法转换可以通过调用特定的方法来实现类型转换。例如:

C#
string str = "123"; if (int.TryParse(str, out int i)) { Console.WriteLine($"Successfully converted to int: {i}"); } else { Console.WriteLine("Conversion failed"); }

7.3 泛型方法中的类型转换

在泛型方法中进行类型转换可以提高代码的复用性和灵活性:

C#
public T ConvertType<T>(object input) { try { return (T)Convert.ChangeType(input, typeof(T)); } catch (InvalidCastException) { return default(T); } } // 使用示例 object obj = "456"; int result = ConvertType<int>(obj); Console.WriteLine($"Converted result: {result}");

7.4 使用反射进行类型转换

反射可以让你在运行时动态地进行类型转换:

C#
public class MyClass { public int MyProperty { get; set; } } // 使用反射进行类型转换 object obj = new MyClass { MyProperty = 123 }; Type type = obj.GetType(); PropertyInfo propertyInfo = type.GetProperty("MyProperty"); if (propertyInfo != null) { int value = (int)propertyInfo.GetValue(obj); Console.WriteLine($"Property value: {value}"); }

输出:

image.png

结论

类型转换是C#中一个强大而灵活的特性。通过掌握隐式转换、显式转换、asis 操作符,以及自定义类型转换,你可以更有效地处理不同类型之间的数据转换。记住,在进行类型转换时要小心,确保不会丢失重要的数据,并始终考虑使用安全的转换方法来避免运行时异常。希望这些高级技巧能帮助你在C#编程中更加游刃有余。

本文作者:rick

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!