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

20250528-C#知识:强制类型转换

C#知识:强制类型转换

本文介绍一下C#中几种常用的类型转换方式,这里不介绍隐式转换。


1、括号强转

  • 可能会丢失精度。
            //强制类型转换//会丢失精度double a = 3.5;int b = (int)a;Console.WriteLine(b);   //3

2、将字符串转换为其他类型(Parse)

  • 无法将float的字符串表示用int.Parse()转换为int型数。
            //Parse方法转换//将字符串转换为其他类型string str = "123";int c = int.Parse(str);Console.WriteLine(c);   //123//假设字符串的内容为非int类型,但还是用Parse方法将字符串转成intstring str2 = "356d";int d = int.Parse(str2);Console.WriteLine(d);   //Unhandled exception. System.FormatException: The input string '356d' was not in a correct format.//也可以将字符串转换为其他类型string str3 = "123.456789";float e = float.Parse(str3);Console.WriteLine(e);   //123.45679//将浮点数字符串转成整数试试string str4 = "123.45";int f = int.Parse(str4);Console.WriteLine(f);   //Unhandled exception. System.FormatException: The input string '123.45' was not in a correct format.

3、 其他类型转String(ToString方法)

  • 可以将其他类型数据转换为字符串
  • 任何类都是Object类的子类,直接输出变量时一般会调用变量类型的ToString方法进行输出
            //ToString方法//可以将其他类型数据转换为字符串//任何类都是Object类的子类,直接输出变量时一般会调用变量类型的ToString方法进行输出float g = 3.1415926f;Console.WriteLine(g.ToString());    //3.1415925 精度丢失了Console.WriteLine(g);   //3.1415925

4、Convert方法

  • 泛用性比较强,基本支持各种内置类型的相互转换
  • 支持强转,可以将float数转换成int数
  • 和Parse方法一样,也无法将float的字符串转换为int型数。
            //Convert方法//泛用性比较强//将字符串转成intstring str5 = "123";int h = Convert.ToInt32(str5);Console.WriteLine(h);   //123//将int转成字符串string str6 = Convert.ToString(h);Console.WriteLine(str6.GetType());  //System.String//将float转换为intfloat i = 3.56f;int j = Convert.ToInt32(i);Console.WriteLine(j);   //4 看来会四舍五入强制将float转出int//试试将float字符串转为intstring str7 = "3.56";int k = Convert.ToInt32(str7);  //Unhandled exception. System.FormatException: The input string '3.56' was not in a correct format.Console.WriteLine(k);

5、完整代码示例:

namespace ForceClassConvert
{internal class Program{static void Main(string[] args){//强制类型转换//会丢失精度double a = 3.5;int b = (int)a;Console.WriteLine(b);   //3//Parse方法转换//将字符串转换为其他类型string str = "123";int c = int.Parse(str);Console.WriteLine(c);   //123//假设字符串的内容为非int类型,但还是用Parse方法将字符串转成intstring str2 = "356d";int d = int.Parse(str2);  //Unhandled exception. System.FormatException: The input string '356d' was not in a correct format.Console.WriteLine(d);//也可以将字符串转换为其他类型string str3 = "123.456789";float e = float.Parse(str3);Console.WriteLine(e);   //123.45679//将浮点数字符串转成整数试试string str4 = "123.45";int f = int.Parse(str4);  //Unhandled exception. System.FormatException: The input string '123.45' was not in a correct format.Console.WriteLine(f);//ToString方法//可以将其他类型数据转换为字符串//任何类都是Object类的子类,直接输出变量时一般会调用变量类型的ToString方法进行输出float g = 3.1415926f;Console.WriteLine(g.ToString());    //3.1415925 精度丢失了Console.WriteLine(g);   //3.1415925//Convert方法//泛用性比较强//将字符串转成intstring str5 = "123";int h = Convert.ToInt32(str5);Console.WriteLine(h);   //123//将int转成字符串string str6 = Convert.ToString(h);Console.WriteLine(str6.GetType());  //System.String//将float转换为intfloat i = 3.56f;int j = Convert.ToInt32(i);Console.WriteLine(j);   //4 看来会四舍五入强制将float转出int//试试将float字符串转为intstring str7 = "3.56";int k = Convert.ToInt32(str7);  //Unhandled exception. System.FormatException: The input string '3.56' was not in a correct format.Console.WriteLine(k);}}
}

6、参考资料:

  1. 《唐老狮C#入门》:主要知识点

本篇结束,感谢您的阅读~

在这里插入图片描述

http://www.xdnf.cn/news/9643.html

相关文章:

  • PostgreSQL 数据完整性检查工具对比:amcheck 与 pg_checksums
  • TCP连接数统计脚本
  • 【系统架构设计师】2025年上半年真题论文回忆版: 论系统负载均衡设计方法(包括解题思路和参考素材)
  • 电子电路:压降的定义与原理
  • 在 Ubuntu 上挂载其他硬盘的步骤
  • uniapp 实战demo
  • AI赋能金融风控:基于机器学习的智能欺诈检测系统实战教程
  • SQL中各个子句的执行顺序
  • SpringBoot中解决跨域问题
  • Next.js 15 与 Apollo Client 的现代集成及性能优化
  • Web3 风控挑战重重,图数据库为何成为破局关键-悦数图数据库
  • Microsoft 推出 Magentic-UI,多智能体引领网页人机协作变革
  • Step9—Ambari Web UI 初始化安装 (Ambari3.0.0)
  • 用豆包写单元测试
  • Typescript学习教程,从入门到精通,TypeScript 泛型与类型操作详解(一)(16)
  • 2025河北秦皇岛CCPC【部分题解】
  • 开发手记:Vue 3 卷轴展开动画组件的实现与思考
  • Golang | 代理模式
  • 端口映射不通的原因有哪些?路由器设置后公网访问本地内网失败分析
  • 农业光合参数反演专栏
  • CSP 2024 提高级第一轮(CSP-S 2024)阅读程序第一题解析
  • 【Docker】技术架构演进
  • failed to bind host port for 0.0.0.0:3306
  • 【 Docker系列】 Docker部署kafka
  • 办公效率王Word批量转PDF 50 +文档一键转换保留原格式零错乱
  • GC1267F单相全波风扇电机预驱动器芯片详解
  • 精益数据分析(93/126):增长率的真相——从数据基准到科学增长策略
  • Linux 中常见的安全与权限机制
  • Vim常用快捷键
  • element-plus主题换色