类型转换汇总 之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>();
相关推荐
qq_3482318516 小时前
Spring Boot开发过程中常见问题
java·spring boot·后端
程序修理员16 小时前
java+vue实现文件下载进度条
java·开发语言·vue.js
毕设源码-赖学姐16 小时前
【开题答辩全过程】以 高校教师管理系统设计与实现为例,包含答辩的问题和答案
java·eclipse
不会代码的小猴16 小时前
C++的第十一天笔记
java·前端·jvm
雨中飘荡的记忆16 小时前
Javassist实战
java
数据门徒16 小时前
《人工智能现代方法(第4版)》 第8章 一阶逻辑 学习笔记
人工智能·笔记·学习·算法
陈文锦丫16 小时前
微服务-----
java·数据库·微服务
任子菲阳16 小时前
学Java第五十三天——IO综合练习(1)
java·开发语言·爬虫
繁华似锦respect16 小时前
单例模式出现多个单例怎么确定初始化顺序?
java·开发语言·c++·单例模式·设计模式·哈希算法·散列表