C#判斷數(shù)據(jù)類型相關(guān)代碼實(shí)例
作者:佚名
這是一篇C#判斷數(shù)據(jù)類型相關(guān)代碼實(shí)例,通過這段代碼,可以實(shí)現(xiàn)C#判斷數(shù)據(jù)類型,希望本文對(duì)大家有所幫助。
通過這段代碼,可以實(shí)現(xiàn)C#判斷數(shù)據(jù)類型
- 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#判斷數(shù)據(jù)類型
- int i = 5;
- string k="哈,今天的天氣不錯(cuò),我叫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#判斷數(shù)據(jù)類型相關(guān)代碼實(shí)例就介紹到這里。
【編輯推薦】
責(zé)任編輯:彭凡
來源:
aixq.com