1、Convert.ToInt32(string s)
这个方法的返回值是int类型,要用int类型的变量接收
如:
string strNum=Console.ReadLine();int age=Convert.ToInt32(strNum);
或者:
int age=Convert.ToInt32(“18”);
若输入的不是整形数字,则四舍五入
int age=Convert.ToInt32(“18.8”);//这里age=19
2、int.Parse(string s)
输入的字符串类型必须为int型
如:
string strNum=Console.ReadLine();int age=int.Parse(strNum);
3、int.TryParse(string s,out int result)
这个方法的返回值是bool类型的,int.TryParse()要用bool类型的变量接收
如:
string strNum=Console.ReadLine();int age=0;bool result=int.TryParse(strNum,out age);
另外:这里以int类型为例,而double、float与int类似。
爱站程序员基地
![(原创)[C#] 一步一步自定义拖拽(Drag&Drop)时的鼠标效果:(一)基本原理及基本实现-爱站程序员基地](https://aiznh.com/wp-content/uploads/2022/07/20220721231016-62d9dcd85c40b-220x150.gif)

