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方法将字符串转成int
            string 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方法
            //泛用性比较强
            //将字符串转成int
            string str5 = "123";
            int h = Convert.ToInt32(str5);
            Console.WriteLine(h);   //123
            //将int转成字符串
            string str6 = Convert.ToString(h);
            Console.WriteLine(str6.GetType());  //System.String
            //将float转换为int
            float i = 3.56f;
            int j = Convert.ToInt32(i);
            Console.WriteLine(j);   //4 看来会四舍五入强制将float转出int
            //试试将float字符串转为int
            string 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方法将字符串转成int
            string 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方法
            //泛用性比较强
            //将字符串转成int
            string str5 = "123";
            int h = Convert.ToInt32(str5);
            Console.WriteLine(h);   //123
            //将int转成字符串
            string str6 = Convert.ToString(h);
            Console.WriteLine(str6.GetType());  //System.String
            //将float转换为int
            float i = 3.56f;
            int j = Convert.ToInt32(i);
            Console.WriteLine(j);   //4 看来会四舍五入强制将float转出int
            //试试将float字符串转为int
            string 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#入门》:主要知识点

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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值