类型转换汇总 之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>();
相关推荐
墨咖5 小时前
java实现NTP服务以及服务调用端(Client)功能
java·开发语言·时间同步·ntp·时钟源同步
小老鼠不吃猫6 小时前
C++ STL <algorithm>中泛型算法:查找、排序、修改、统计、生成
c++·算法·排序算法
zl9798996 小时前
SpringBoot-Web开发之请求参数处理
java·spring boot
新建文件夹-6 小时前
深入浅出Langchain4j——构建Java大语言模型应用的新范式
java·开发语言·语言模型
絔宝6 小时前
Eclipse控制台乱码解决方式
java·ide·eclipse
白杆杆红伞伞6 小时前
01_svm_二分类
算法·支持向量机·分类
isyoungboy6 小时前
使用SVM构建光照鲁棒的颜色分类器:从特征提取到SVM
算法·机器学习·支持向量机
极客数模6 小时前
2025年MathorCup 大数据竞赛明日开赛,注意事项!论文提交规范、模板、承诺书正确使用!2025年第六届MathorCup数学应用挑战赛——大数据竞赛
大数据·python·算法·matlab·图论·比赛推荐
.小小陈.6 小时前
数据结构3:复杂度
c语言·开发语言·数据结构·笔记·学习·算法·visual studio