类型转换汇总 之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>();
相关推荐
无糖冰可乐212 小时前
IDEA多java版本切换
java·ide·intellij-idea
合作小小程序员小小店2 小时前
web开发,在线%超市销售%管理系统,基于idea,html,jsp,java,ssh,sql server数据库。
java·前端·sqlserver·ssh·intellij-idea
brucelee1862 小时前
IntelliJ IDEA 设置 Local History 永久保留
java·ide·intellij-idea
Pluto_CSND4 小时前
Java中的静态代理与动态代理(Proxy.newProxyInstance)
java·开发语言
百***46455 小时前
Java进阶-在Ubuntu上部署SpringBoot应用
java·spring boot·ubuntu
serve the people5 小时前
Prompts for Chat Models in LangChain
java·linux·langchain
一叶飘零_sweeeet5 小时前
不止于 API 调用:解锁 Java 工具类设计的三重境界 —— 可复用性、线程安全与性能优化
java·工具类
cynicme6 小时前
力扣3228——将 1 移动到末尾的最大操作次数
算法·leetcode
熬了夜的程序员6 小时前
【LeetCode】109. 有序链表转换二叉搜索树
数据结构·算法·leetcode·链表·职场和发展·深度优先
獨枭6 小时前
C# 本地项目引用失效与恢复全攻略
开发语言·c#·visual studio