当前位置: 首页 > news >正文

类型转换汇总 之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>();

http://www.dtcms.com/a/519736.html

相关文章:

  • Python列表操作、条件判断、循环、函数定义
  • ps怎么在dw上做网站广州番禺发布网
  • UltraScale/+ FPGA实现万兆网的两种方式:GT核、10G Ethernet Subsystem核
  • 元宇宙重构社交生态:从平面互动到沉浸式情感连接
  • SaaS模式重构工业软件竞争规则,助力中小企业快速实现数字化转型
  • 利用AI大模型重构陈旧代码库 (Refactoring Legacy Codebase with AI)
  • 数字孪生技术 重构 智能仓储新生态
  • 从上下文工程到组织知识管理重构——HRBP在人机协同时代的新使命
  • 1.2亿老人需助听器:本土品牌如何以AI破局,重构巨头垄断市场?
  • DeepSeek-OCR本地部署教程:DeepSeek突破性开创上下文光学压缩,10倍效率重构文本处理范式
  • “AI导师”现象:生成式人工智能对教育主体性及教学范式的重构
  • 住房和城乡建设统计网站南沙滩做网站公司
  • 网站备案要啥公司网站建设的视频教程
  • 全平台内容排期与矩阵玩法
  • 基于Python的声音信号分类:从公开数据集到自定义音频的实践
  • Web 前端开发常用工具推荐与团队实践分享
  • 网站 mssql 数据库2023八月重点新闻事件
  • [特殊字符] 多环境 DCDN 缓存与 version 切换刷新方案
  • PY32F040单片机介绍(2)
  • WordPress 迭代演进总结
  • MongoDB 排序操作详解sort方法使用指南
  • 深圳p2p网站开发如何把网站扒下来
  • 网站制作苏州企业舟山市建设工程造价管理协会网站
  • golang学习笔记:标准库strconv
  • A Better Finder Rename for mac 文件批量重命名
  • xss-labs pass-11
  • 深入剖析 iOS 26 系统流畅度,多工具协同监控与性能优化实践
  • 解决升级IDEA2025.2后,每次打开Maven项目爆红的问题:Windows和Mac解决方案
  • 家用豆腐磨浆机设计
  • 六级单词03