C#判断数据类型相关代码实例

通过这段代码,可以实现C#判断数据类型

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. public class Test2  
  6. {  
  7.   public void aaB(string a, int b)  
  8.         {  
  9.             if(a!=null)  
  10.       {  
  11.         Console.WriteLine("The stirng  is " + a + ",  the number is " + b);  
  12.       }  
  13.       else  
  14.       {  
  15.         Console.WriteLine("error");  
  16.       }  
  17.         }  
  18.   public void checkType(object type)  
  19.   {  
  20.         Console.WriteLine("The "+type+" type is {0},", type.GetType());  
  21.   }  
  22.   public static void Main()  
  23.   {  
  24.     Test2 ts = new Test2();  
  25.     string a="my name is a";  
  26.     int b=3662296;  
  27.     ts.aaB(a,b);  //C#判断数据类型
  28.     int   i   =   5;    
  29.     string k="哈,今天的天氣不錯,我叫string ";  
  30.     Console.WriteLine("i   is   an   int   ?   {0}" , i.GetType() == typeof(int));   
  31.     Console.WriteLine("i   is   an   int   ?   {0}" , typeof(int).IsInstanceOfType(i));  
  32.     Console.WriteLine("k   is   an   int   ?   {0}" , typeof(int).IsInstanceOfType(k));  
  33.     Console.WriteLine("The type of k is {0},",k.GetType());  
  34.     ts.checkType(k);  
  35.     ts.checkType(i);  
  36.    }   

C#判断数据类型相关代码实例就介绍到这里。

【编辑推荐】

  1. C#创建Windows服务学习的一点体会
  2. C#Windows服务程序之添加安装程序图解
  3. C#Windows服务程序开发实例浅析
  4. C#Windows服务程序开发浅析
  5. C#Windows服务程序的快速开发
THE END