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、参考资料:
- 《唐老狮C#入门》:主要知识点
本篇结束,感谢您的阅读~