关于int.TryParse的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
string TP = "123abc";
string TPE = "";
int re,ret;
//测试转换失败
if (int.TryParse(TP, out re) == true)
{
Console.WriteLine("{0}能转换成功,转换后的数为:{1}",TP,re );
}
else
{
Console.WriteLine("{0}转换失败",TP);
}
//暂停
Console.ReadKey();
Console.WriteLine();
//测试转换成功
if (int.TryParse(TPE, out ret) == true)
{
Console.WriteLine("{0}能转换成功,转换后的数为:{1}" ,TPE,ret);
}
else
{
Console.WriteLine("{0}转换失败",TPE);
}
//暂停
Console.ReadKey();
}
}
}
实现int.TryParse的原理:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
int re;
string s = "";
if (IntTryParse(s, out re))
{
Console.WriteLine("转换成功!" + re);
Console.ReadKey();
}
else
{
Console.WriteLine("转换失败!");
Console.ReadKey();
}
} static bool IntTryParse(string s, out int result)//模仿TryParse定义方法IntTryParse
{
result = ;
try
{
result = Convert.ToInt32(s);
return true;
}
catch
{
return false;
}
}
}
}
关于int.TryParse的使用的更多相关文章
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别 <转>
作者:Statmoon 出处:http://leolis.cnblogs.com/ 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为整型(int)来讲,有四种方法 ...
- int.Parse()、int.TryParse()和Convert.ToInt32()的区别
1:int.Parse(一个参数) 此参数必须满足: 1 只能是字符串: 2 只能是 “整型” 字符串,即各种整型ToString()之后的形式,也不能为浮点型. 2:int.TryPa ...
- (int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
C#中(int).int.Parse().int.TryParse()和Convert.ToInt32()的区别 原文链接:http://www.cnblogs.com/leolis/p/3968 ...
- Convert.ToInt32,int.Parse,int.TryParse,(int)的区别
1 (int)变量名[强制类型转换] 该转换方式主要用于数字类型转换,从int类型到long,float,double,decimal类型,可以使用隐式转换,但是从long类型到int类型就需要使用显 ...
- C#中 int.TryParse 的用法
int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = in ...
- C# int.Parse()与int.TryParse()
int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = in ...
- C# int.Parse()、int.TryParse()与Convert.ToInt32()的区别
1.(int)是一种类型转换:当我们觟nt类型到long,float,double,decimal类型,可以使用隐式转换,但是当我们从long类型到int类型就需要使用显式转换,否则会产生编译错误. ...
- int.Parse()与int.TryParse()
int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = ...
- int.Tryparse() 、int.parse()、Convert.To32() 的区别
int.Tryparse() Int32.TryParse(source, result)则无论如何都不抛出异常,只会返回true或false来说明解析是否成功,如果解析失败,调用方将会得到0值. ...
随机推荐
- BZOJ 1211: [HNOI2004]树的计数( 组合数学 )
知道prufer序列就能写...就是求个可重集的排列...先判掉奇怪的情况, 然后答案是(N-2)!/π(d[i]-1)! -------------------------------------- ...
- Spring+Maven配置等问题
1. 在POM中使用 统一/指定/特定 的Spring版本号 <properties> <spring.version>4.1.6.RELEASE</spring.ver ...
- 帝国cms分页样式修改文件-注意事项
帝国cms分页样式主要有:内容页分页样式.列表页分页样式以及默认搜索模板使用的搜索样式等几种. 要改这些样式其实也很简单,在网站目录中找到相应的.css文件修改empages属性就行了,但是这样比较麻 ...
- c# zip file and folder programmatically
In .net 4.5 Framework, we can zip a file by this way: private static string CompressFile(string sour ...
- ThinkPHP 3.1.2 模板中的基本语法<1>
# # ThinkPHP 3.1.2 模板中的基本语法 一.传统的方式,导入CSS和JS文件 1.css link js scr <link rel='stylesheet' type='tex ...
- 宣布与 NBC 合作直播索契冬季奥运
奥运开始的那天早些时候,NBC 和 Microsoft 宣布选择 Windows Azure 媒体服务为 2014 年俄罗斯索契冬奥会提供现场直播.与以往不同,以往的冬奥会采用了有限的流,但本届 ...
- Shared_from_this 几个值得注意的地方
shared_from_this()是enable_shared_from_this<T>的成员 函数,返回shared_ptr<T>.首先需要注意的是,这个函数仅在share ...
- Visual Studio Tools for Unity安装及使用
Visual Studio Tools for Unity安装及使用 转载自:CSDN 晃了一下,10.1到现在又过去两个月了,这两个月什么也没有学,整天上班下班,从这周末开始拾起unity,为了年后 ...
- Canvas使用渐变之-径向渐变详解
创建径向渐变使用 createRadialGrdient(x0,y0,r0,x1,y1,r1) 一共六个参数,分别代表: 起点的圆心坐标(第一个和第二个参数), 起点园的半径(第三个参数), 终点 ...
- JavaScript基础知识----document对象
对象属性document.title //设置文档标题等价于HTML的<title>标签document.bgColor //设 ...