关于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值. ...
随机推荐
- 两种mysql文件安装方式——win7 32位OS
官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的. 1. .ZIP格式安装 http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345 ...
- Spring学习之Aop的基本概念
转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...
- PrintWriter与outputStream区别
网上截取: printWriter:我们一般用来传的是对像 而outputStream用来传的是二进制,故上传文件时,一定要使用此. PrintWriter以字符为单位,支持汉字,OutputStre ...
- java list基本用法
List<E>([]内的内容可省略),与数组类似: 实例化:List[<数据类型>] list = new ArrayList[<数据类型>](); 获得集合内元素 ...
- Python 安装、循环语句、数据类型(一)
一.关于版本的选择 Should i use Python 2 or Python 3 for my development activity?转载自Python官网 Short version: P ...
- 以程序的方式操纵NTFS的文件权限(陈皓)
http://blog.csdn.net/haoel/article/details/2905 http://blog.sina.com.cn/s/blog_7f91494101018nmn.html
- perl5 第八章 子程序
第八章 子程序 by flamephoenix 一.定义二.调用 1.用&调用 2.先定义后调用 3.前向引用 4.用do调用三.返回值四.局部变量五.子程序参数传递 1.形式 2 ...
- BZOJ 2016: [Usaco2010]Chocolate Eating
题目 2016: [Usaco2010]Chocolate Eating Time Limit: 10 Sec Memory Limit: 162 MB Description 贝西从大牛那里收到了 ...
- Linux常用命令总结——文件管理
Linux中的目录 路径:也就是linux中的目录(文件夹)有绝对路径和相对路径 根目录:/ 用户主目录(home directory):位于/home目录下,用户登录时 工作目录(working d ...
- 【在网页中获取截图数据】Chrome和Firefox下的实战经验
[转载自我在segmentfault的专栏:https://segmentfault.com/a/1190000004584071] 最近在实现一个功能,需求如下: 前提:当前页面无弹窗 页面任意位置 ...