1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication7 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string TP = "123abc"; 14 string TPE = "123"; 15 int re,ret; 16 //测试转换失败 17 if (int.TryParse(TP, out re) == true) 18 { 19 Console.WriteLine("{0}能转换成功,转换后的数为:{1}",TP,re ); 20 } 21 else 22 { 23 Console.WriteLine("{0}转换失败",TP); 24 } 25 //暂停 26 Console.ReadKey(); 27 Console.WriteLine(); 28 //测试转换成功 29 if (int.TryParse(TPE, out ret) == true) 30 { 31 Console.WriteLine("{0}能转换成功,转换后的数为:{1}" ,TPE,ret); 32 } 33 else 34 { 35 Console.WriteLine("{0}转换失败",TPE); 36 } 37 //暂停 38 Console.ReadKey(); 39 } 40 } 41 }
---恢复内容结束---
原文:http://www.cnblogs.com/start-from-scratch/p/5056675.html