类型转换汇总 之C#

1. 隐式转换(Implicit Conversion)

​特点​​:自动进行,不需要特殊语法,不会丢失数据

复制代码
// 数值类型(小范围到大范围)
int intValue = 100;
long longValue = intValue;  // 隐式转换

// 派生类到基类
string str = "hello";
object obj = str;  // 隐式转换

// 自定义隐式转换
public class Celsius
{
    public double Temperature { get; set; }
    
    public static implicit operator Fahrenheit(Celsius c)
    {
        return new Fahrenheit { Temperature = c.Temperature * 9 / 5 + 32 };
    }
}

Celsius celsius = new Celsius { Temperature = 100 };
Fahrenheit fahrenheit = celsius;  // 隐式转换

2. 显式转换(Explicit Conversion)

​特点​​:需要强制转换运算符,可能丢失数据或抛出异常

2.1 基本类型显式转换

复制代码
// 数值类型(大范围到小范围)
double doubleValue = 99.99;
int intValue = (int)doubleValue;  // 显式转换,丢失小数部分

// 引用类型向下转换
object obj = "hello";
string str = (string)obj;  // 显式转换,可能抛出 InvalidCastException

// 使用 as 运算符(安全转换,失败返回 null)
object obj2 = "test";
string str2 = obj2 as string;  // 成功返回 string,失败返回 null
if (str2 != null) { /* 使用 str2 */ }

2.2 自定义显式转换

复制代码
public class Fahrenheit
{
    public double Temperature { get; set; }
    
    public static explicit operator Celsius(Fahrenheit f)
    {
        return new Celsius { Temperature = (f.Temperature - 32) * 5 / 9 };
    }
}

Fahrenheit fahr = new Fahrenheit { Temperature = 212 };
Celsius celsius = (Celsius)fahr;  // 显式转换

3. 使用 Convert 类

​特点​​:提供丰富的类型转换方法,处理 null 值和边界情况

复制代码
// 字符串到数值类型
string numberStr = "123";
int number = Convert.ToInt32(numberStr);

// 处理 null 值
object nullObj = null;
int result = Convert.ToInt32(nullObj);  // 返回 0,不抛异常

// 各种类型转换
bool boolVal = Convert.ToBoolean("true");
double doubleVal = Convert.ToDouble("99.99");
DateTime dateVal = Convert.ToDateTime("2024-01-01");

// 进制转换
string binary = Convert.ToString(255, 2);  // "11111111"
string hex = Convert.ToString(255, 16);    // "ff"

4. Parse 和 TryParse 方法

4.1 Parse 方法

复制代码
// 可能抛出 FormatException, OverflowException
int number = int.Parse("123");
double value = double.Parse("99.99");
DateTime date = DateTime.Parse("2024-01-01");

4.2 TryParse 方法(推荐)

复制代码
// 安全转换,不抛异常
if (int.TryParse("123", out int result))
{
    Console.WriteLine($"转换成功: {result}");
}
else
{
    Console.WriteLine("转换失败");
}

// 多个尝试
string[] values = { "123", "abc", "456" };
foreach (string value in values)
{
    if (int.TryParse(value, out int num))
    {
        Console.WriteLine($"成功: {num}");
    }
}

5. 类型检查运算符

5.1 is 运算符

复制代码
object obj = "hello";

if (obj is string str)
{
    Console.WriteLine($"是字符串: {str}");  // 直接使用转换后的变量
}

if (obj is int number)
{
    Console.WriteLine($"是数字: {number}");
}
else
{
    Console.WriteLine("不是数字");
}

5.2 as 运算符

复制代码
object obj = "hello";
string str = obj as string;

if (str != null)
{
    Console.WriteLine($"转换成功: {str}");
}

// 与 null 合并运算符结合
string result = (obj as string) ?? "默认值";

6. 装箱和拆箱(Boxing/Unboxing)

复制代码
// 装箱:值类型 → 引用类型
int number = 42;
object boxed = number;  // 装箱

// 拆箱:引用类型 → 值类型
int unboxed = (int)boxed;  // 拆箱

// 错误示例(会抛出 InvalidCastException)
object wrongBoxed = "hello";
// int wrongUnboxed = (int)wrongBoxed;  // 运行时错误!

7. 使用泛型转换

复制代码
// 泛型方法处理多种类型
public static T ConvertValue<T>(object value)
{
    return (T)Convert.ChangeType(value, typeof(T));
}

// 使用
int number = ConvertValue<int>("123");
double value = ConvertValue<double>("99.99");
bool flag = ConvertValue<bool>("true");

8. 模式匹配(C# 7.0+)

复制代码
public static void ProcessObject(object obj)
{
    switch (obj)
    {
        case int i when i > 100:
            Console.WriteLine($"大数字: {i}");
            break;
        case int i:
            Console.WriteLine($"小数字: {i}");
            break;
        case string s:
            Console.WriteLine($"字符串: {s}");
            break;
        case null:
            Console.WriteLine("空值");
            break;
        default:
            Console.WriteLine("未知类型");
            break;
    }
}

9. 最佳实践总结

✅ 推荐做法:

复制代码
// 1. 使用 TryParse 进行字符串到数值的转换
if (int.TryParse(input, out int result)) { }

// 2. 使用 as 运算符进行安全引用类型转换
var str = obj as string;
if (str != null) { }

// 3. 使用 is 进行类型检查和模式匹配
if (obj is string s) { }

// 4. 使用 Convert 类处理边界情况
int value = Convert.ToInt32(maybeNull);

❌ 避免做法:

复制代码
// 1. 避免直接 Parse(可能抛异常)
// int value = int.Parse(userInput);

// 2. 避免不安全的强制转换
// string str = (string)someObject;

// 3. 避免无效的拆箱
// int num = (int)someObject;  // 可能不是 int

10. 实用扩展方法

复制代码
public static class ConversionExtensions
{
    public static int ToInt(this string value, int defaultValue = 0)
    {
        return int.TryParse(value, out int result) ? result : defaultValue;
    }
    
    public static T To<T>(this object value, T defaultValue = default)
    {
        try
        {
            return (T)Convert.ChangeType(value, typeof(T));
        }
        catch
        {
            return defaultValue;
        }
    }
}

// 使用扩展方法
string input = "123";
int number = input.ToInt(-1);  // 失败返回 -1
double value = "99.99".To<double>();
相关推荐
专吃海绵宝宝菠萝屋的派大星15 小时前
使用Dify对接自己开发的mcp
java·服务器·前端
小李子呢021115 小时前
前端八股6---v-model双向绑定
前端·javascript·算法
yongui4783415 小时前
C# 与三菱PLC通讯解决方案
开发语言·c#
大数据新鸟15 小时前
操作系统之虚拟内存
java·服务器·网络
Tong Z15 小时前
常见的限流算法和实现原理
java·开发语言
凭君语未可15 小时前
Java 中的实现类是什么
java·开发语言
He少年15 小时前
【基础知识、Skill、Rules和MCP案例介绍】
java·前端·python
克里斯蒂亚诺更新16 小时前
myeclipse的pojie
java·ide·myeclipse
迷藏49416 小时前
**eBPF实战进阶:从零构建网络流量监控与过滤系统**在现代云原生架构中,**网络可观测性**和**安全隔离**已成为
java·网络·python·云原生·架构
迷藏49416 小时前
**发散创新:基于Solid协议的Web3.0去中心化身份认证系统实战解析**在Web3.
java·python·web3·去中心化·区块链