C?sharp?(#)?數(shù)據(jù)類型獲取方式
C sharp (#) 數(shù)據(jù)類型獲取
這里研究一下關(guān)于c#中如何獲取變量類型的問(wèn)題。
首先我們研究一下如何獲取單個(gè)變量的類型
// 問(wèn)題一:獲取單個(gè)變量的類型
// 方法一:使用GetType()方法
public static void JudgeType()
{
? ? int element = 5;
? ? // 我們應(yīng)該知道, GetType()會(huì)返回一個(gè)類型,因此我們需要用類型變量來(lái)存儲(chǔ)它
? ? Type type = element.GetType();
? ? // 如果我們需要判斷這個(gè)類型與其他的類型,比如與int類型,那么我們應(yīng)該與typeof(int)進(jìn)行比較
? ? if (type == typeof(int))
? ? {
? ? ? ? Console.WriteLine("Is the type of element int? {0}", "Yes");
? ? }
}
// =============================================
// 方法二:使用is方法
public static void JudgeType()
{
? ? // 這里為了避免warning的出現(xiàn),我們使用object來(lái)定義變量
? ? object element = 5;
? ? // 使用is來(lái)直接判斷變量的類型
? ? if (element is int)
? ? {
? ? ? ? Console.WriteLine("Is the type of element int? {0}", "Yes");
? ? }
}接下來(lái)我們研究一下如何獲取列表變量的類型
// 問(wèn)題二: 獲取列表的類型
// 方法一:使用GetType()方法
public static void JudgeType()
{
? ? // 創(chuàng)建一個(gè)列表對(duì)象
? ? 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#類型的派生譜類

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用NPOI對(duì)word進(jìn)行讀寫(xiě)
這篇文章介紹了C#使用NPOI對(duì)word進(jìn)行讀寫(xiě)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Quartz.Net實(shí)現(xiàn)原理及使用方法詳解
這篇文章主要介紹了Quartz.Net實(shí)現(xiàn)原理及使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
C# Directory.GetFiles()函數(shù)案例詳解
這篇文章主要介紹了C# Directory.GetFiles()函數(shù)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C#實(shí)現(xiàn)windows系統(tǒng)重啟和關(guān)機(jī)的代碼詳解
這篇文章主要介紹了C#實(shí)現(xiàn)windows系統(tǒng)重啟和關(guān)機(jī)的的方法,涉及C#調(diào)用windows系統(tǒng)命令實(shí)現(xiàn)控制開(kāi)機(jī)、關(guān)機(jī)等操作的技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2024-02-02
C#小知識(shí)之有趣的類型靜態(tài)構(gòu)造器
這篇文章主要介紹了C#小知識(shí)之有趣的類型靜態(tài)構(gòu)造器,本文直接給分實(shí)例代碼,然后分析了C#中的這一個(gè)有趣的現(xiàn)象,需要的朋友可以參考下2015-04-04

