关于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值. ...
随机推荐
- Struts学习之集成Ajax
转自:http://blog.csdn.net/hanxuemin12345/article/details/38782213 一,引题 1,Json数据格式简介 JSON是脱离语言的理想的数据交换格 ...
- appium获取app应用的package和 activity。---新手总结(大牛勿喷,新手互相交流)
从网上搜索的方法: 如下: 1.查看源码 2.日志法a.启动待测apkb.开启日志输出:adb logcat>D:/log.txtc.关闭日志输出:ctrl+cd.查看日志直接搜索 :Displ ...
- 24_Core Data Demo
今天开始学习Core Data,类似于数据库,可以永久保存数据.不过当把App从iPhone删掉之后就没有了.可以用来保存App的运行数据. 参考链接:iOS Swift教程 Core Data 概述 ...
- Spring RESTful服务接收和返回JSON最佳实践
http://blog.csdn.net/prince_hua/article/details/12103501
- Python CSV文件处理/读写及With as 用法
可以不使用CSV模块 逐行处理: for line in open("samples/sample.csv"): title, year, director = line.spli ...
- 再探java基础——break和continue的用法
再探java基础——break和continue的用法 break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后 ...
- PLSQL 导入表到Oracle------》从一个表空间导入到其它表空间
在用PLSQL导入.dmp文件到Oracle时出现的问题如下: Import started on 2015/11/18 10:42:44E:\oracle\product\10.2.0\db ...
- java实现冒泡排序,选择排序,插入排序,快速排序(简洁版)及性能测试
1.冒泡排序是排序里面最简单的了,但性能也最差,数量小的时候还可以,数量一多,是非常慢的. 它的时间复杂度是O(n*n),空间复杂度是O(1) 代码如下,很好理解. public void bubbl ...
- 子请求执行失败。有关更多信息,请检查 InnerException。
异常:子请求执行失败.有关更多信息,请检查 InnerException. 错误:程序请求异常 可能原因: 1.可以检查是否引用了分布视图,而分布视图中发生错误 2.可以检查是否引用了分布视图,而分布 ...
- An Introduction to Interactive Programming in Python
这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲.生动有趣,一共是9周的课程,每一周都会有一个小游戏,经历一遍,对编程会产生很大的兴趣. 所有的程序全部在老师开发的 ...