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

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#入门》:主要知识点

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

在这里插入图片描述

相关文章:

  • Vue2+Vuex通过数组动态生成store数据(扁平模式)
  • AMBA-AHB的地址译码
  • 在线临床指标分类信息表转甜甜圈矩阵图
  • 使用dig查看dns递归查询过程
  • 操作系统 | 第一章:操作系统引论思维导图
  • 【CAPL实战】LIN校验和测试
  • 模型微调参数入门:核心概念与全局视角
  • Step9—Ambari Web UI 初始化安装 (Ambari3.0.0)
  • Proguard代码混淆-springboot3
  • spring4第3课-ioc控制反转-详解依赖注入的4种方式
  • SpringBoot项目快速打包与部署,War包⽅式打包部署与Jar包⽅式打包部署两种方式
  • 基于Vue3.0的【Vis.js】库基本使用教程(002):图片知识图谱的基本构建和设置
  • SpringBoot Controller接收参数方式
  • SpringBoot使用ffmpeg实现视频压缩
  • vue+elementUi+axios实现分页(MyBatis、Servlet)
  • Baklib内容中台驱动资源高效整合
  • matlab中绘图函数plot
  • 《vue.js快速入门》链接摘抄整理
  • DBeaver 连接 OceanBase Oracle 租户
  • vscode 终端 PATH 和python pip 不对
  • 网站在线客服插件代码/北京网络推广公司排行
  • 中文静态网页模板/沈阳网站推广优化
  • 上海网站建设方案/惠州seo快速排名
  • 测速网站怎么做/专业软文平台
  • 淘宝 网站建设教程视频/怎么自己弄一个平台
  • 青岛建设官方网站/正规微商免费推广软件