string line = Console.ReadLine();
try
{
int a=Int32.Parse(line.Trim());
}
catch(Exception ex)
{}
try
{
int a=Int32.Parse(line.Trim());
}
catch(Exception ex)
{}
解决方案 »
- c#引用C++动态链接库
- 急求 导出excel出现的问题
- 麻烦帮教教我,谢谢。"INSERT INTO DGclub部门 (部门编号,部门名称,备注)VALUES("+ "'" + 部门编号.Text.Trim() + "',"+ "'" + 部门名称.Text.Trim() + "',"+ "'" + 备
- XmlDocument如何释放文件
- 菜鸟入门,求救
- 请教:C#网络编程 tcpclient
- vs2005 c#app.config配置文件添加,删除问题~求助
- 为什么 vsto2005安装完成后在vs2005IDE中看不见模板
- [求助]用ftp传输xml文件的问题
- 怎样作一个.net控件,将combox中的下拉部分换成CheckListBox,以实现多选?thanks!
- 怎样用socket请求https页面
- 如何获取窗口上所有的菜单项!
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数");
int a = int.Parse(Console.ReadLine()); //要类型转换
Console.WriteLine("请输入第二个数");
int b = int.Parse(Console.ReadLine());
if (a < b)
{
Console.WriteLine(b);
}
else
{
Console.WriteLine(a);
}
}
}
using System;class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数");
int a = int(Console.ReadLine()); //要类型转换
Console.WriteLine("请输入第二个数");
int b = int(Console.ReadLine());
if (a < b)
{
Console.WriteLine(b);
}
else
{
Console.WriteLine(a);
}
}
}
int a=(int) Console.WriteLine();
int intValue;
第一种:
intValue=Convert.ToInt32(strValue);第二种:
intValue=Int32.Parse(strValue);第三种:
intValue=Int32.TryParse(strValue);三种方法都可以实现string转化为int,但是如果解析失败:
Int32.Parse()总会抛出异常;
Convert.ToInt32()在strValue为null的情况下不会抛出异常而是简单的返回0给调用方;
而Int32.TryParse()则无论如何都不抛出异常,只会返回true或false来说明解析是否成功,如果解析失败,调用方将会得到0值
/////////////////////////////////
显式的类型转换时有一些限制,例如值类型,只能在数字、char类型和enum类型之间转换
不能和引用类型转换!string 为引用类型!
==================================
不行,楼上都解释了很多了