C?sharp?(#)?數(shù)據(jù)類型獲取方式
更新時間:2022年11月03日 10:36:16 作者:勤奮的大熊貓
這篇文章主要介紹了C?sharp?(#)?數(shù)據(jù)類型獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
C sharp (#) 數(shù)據(jù)類型獲取
這里研究一下關(guān)于c#中如何獲取變量類型的問題。
首先我們研究一下如何獲取單個變量的類型
// 問題一:獲取單個變量的類型 // 方法一:使用GetType()方法 public static void JudgeType() { ? ? int element = 5; ? ? // 我們應(yīng)該知道, GetType()會返回一個類型,因此我們需要用類型變量來存儲它 ? ? Type type = element.GetType(); ? ? // 如果我們需要判斷這個類型與其他的類型,比如與int類型,那么我們應(yīng)該與typeof(int)進行比較 ? ? if (type == typeof(int)) ? ? { ? ? ? ? Console.WriteLine("Is the type of element int? {0}", "Yes"); ? ? } } // ============================================= // 方法二:使用is方法 public static void JudgeType() { ? ? // 這里為了避免warning的出現(xiàn),我們使用object來定義變量 ? ? object element = 5; ? ? // 使用is來直接判斷變量的類型 ? ? if (element is int) ? ? { ? ? ? ? Console.WriteLine("Is the type of element int? {0}", "Yes"); ? ? } }
接下來我們研究一下如何獲取列表變量的類型
// 問題二: 獲取列表的類型 // 方法一:使用GetType()方法 public static void JudgeType() { ? ? // 創(chuàng)建一個列表對象 ? ? var list = new List<int>() { 1, 2 }; ? ? Type type = list.GetType(); ? ? if (type == typeof(List<int>)) ? ? { ? ? ? ? Console.WriteLine("Is the type of list List<int>? {0}", "Yes"); ? ? } } // ============================================= // 方法二:使用is方法 public static void JudgeType() { ? ? var list = new List<int>() { 1, 2 }; ? ? if (list is List<int>) ? ? { ? ? ? ? Console.WriteLine("Is the type of list List<int>? {0}", "Yes"); ? ? } } // ============================================= // 方法三:使用GetType()和GetGenericArguments()方法 public static void JudgeType() { ? ? var list = new List<int>() { 1, 2 }; ? ? Type[] type = list.GetType().GetGenericArguments(); ? ? if (type[0] == typeof(int)) ? ? { ? ? ? ? Console.WriteLine("Is the type of list List<int>? {0}", "Yes"); ? ? ? ? Console.WriteLine("Is the type of element in list int? {0}", "Yes"); ? ? } } // ============================================= // 方法四: 使用GetType()和ToString()方法 public static void JudgeType() { ? ? var list = new List<int>() { 1, 2 }; ? ? foreach (var element in list) ? ? { ? ? ? ? Type type1 = element.GetType(); ? ? ? ? if (type1.ToString() == "System.Int32") ? ? ? ? { ? ? ? ? ? ? Console.WriteLine("Is the type of element in list int? {0}", "Yes"); ? ? ? ? } ? ? } } // ============================================= // 方法五: 使用GetType()和Name方法 public static void JudgeType() { ? ? var list = new List<int>() { 1, 2 }; ? ? string type_ = list[0].GetType().Name; ? ? Console.WriteLine(type_); ? ? if (type_ == "Int32") ? ? { ? ? ? ? Console.WriteLine("Is the type of element in list int? {0}", "Yes"); ? ? } }
C#的五大數(shù)據(jù)類型
1.類(class):如Windows,F(xiàn)orm,Console,String
2.結(jié)構(gòu)體(Structures):如Int32,Int64,Single,Double
3.枚舉(Enumerations):如HorizontalAlignment,Visibility
4.接口(Interfaces)
5.委托(Delegates)
C#類型的派生譜類
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C# Directory.GetFiles()函數(shù)案例詳解
這篇文章主要介紹了C# Directory.GetFiles()函數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08C#實現(xiàn)windows系統(tǒng)重啟和關(guān)機的代碼詳解
這篇文章主要介紹了C#實現(xiàn)windows系統(tǒng)重啟和關(guān)機的的方法,涉及C#調(diào)用windows系統(tǒng)命令實現(xiàn)控制開機、關(guān)機等操作的技巧,非常簡單實用,需要的朋友可以參考下2024-02-02