通过这段代码,可以实现C#判断数据类型
- using System;
- using System.Collections.Generic;
- using System.Text;
- public class Test2
- {
- public void aaB(string a, int b)
- {
- if(a!=null)
- {
- Console.WriteLine("The stirng is " + a + ", the number is " + b);
- }
- else
- {
- Console.WriteLine("error");
- }
- }
- public void checkType(object type)
- {
- Console.WriteLine("The "+type+" type is {0},", type.GetType());
- }
- public static void Main()
- {
- Test2 ts = new Test2();
- string a="my name is a";
- int b=3662296;
- ts.aaB(a,b); //C#判断数据类型
- int i = 5;
- string k="哈,今天的天氣不錯,我叫string ";
- Console.WriteLine("i is an int ? {0}" , i.GetType() == typeof(int));
- Console.WriteLine("i is an int ? {0}" , typeof(int).IsInstanceOfType(i));
- Console.WriteLine("k is an int ? {0}" , typeof(int).IsInstanceOfType(k));
- Console.WriteLine("The type of k is {0},",k.GetType());
- ts.checkType(k);
- ts.checkType(i);
- }
- }
C#判断数据类型相关代码实例就介绍到这里。
【编辑推荐】