網站首頁 編程語言 正文
C sharp (#) 數據類型獲取
這里研究一下關于c#中如何獲取變量類型的問題。
首先我們研究一下如何獲取單個變量的類型
// 問題一:獲取單個變量的類型
// 方法一:使用GetType()方法
public static void JudgeType()
{
? ? int element = 5;
? ? // 我們應該知道, GetType()會返回一個類型,因此我們需要用類型變量來存儲它
? ? Type type = element.GetType();
? ? // 如果我們需要判斷這個類型與其他的類型,比如與int類型,那么我們應該與typeof(int)進行比較
? ? if (type == typeof(int))
? ? {
? ? ? ? Console.WriteLine("Is the type of element int? {0}", "Yes");
? ? }
}
// =============================================
// 方法二:使用is方法
public static void JudgeType()
{
? ? // 這里為了避免warning的出現,我們使用object來定義變量
? ? object element = 5;
? ? // 使用is來直接判斷變量的類型
? ? if (element is int)
? ? {
? ? ? ? Console.WriteLine("Is the type of element int? {0}", "Yes");
? ? }
}
接下來我們研究一下如何獲取列表變量的類型
// 問題二: 獲取列表的類型
// 方法一:使用GetType()方法
public static void JudgeType()
{
? ? // 創建一個列表對象
? ? 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#的五大數據類型
1.類(class):如Windows,Form,Console,String
2.結構體(Structures):如Int32,Int64,Single,Double
3.枚舉(Enumerations):如HorizontalAlignment,Visibility
4.接口(Interfaces)
5.委托(Delegates)
C#類型的派生譜類
原文鏈接:https://blog.csdn.net/u011699626/article/details/109164685
相關推薦
- 2023-01-17 解讀python?cvxpy下SDP問題編程_python
- 2024-03-02 Quasar框架使用環境變量聲明接口地址,無需手動判斷
- 2022-10-20 Flutter?StreamBuilder實現局部刷新實例詳解_Android
- 2023-02-06 Python利用Pytorch實現繪制ROC與PR曲線圖_python
- 2022-07-16 SpringMVC @Controller和@RequestMapping注解
- 2023-12-15 IDEA去掉activate-power-mode右上角圖標和Power Mode II 進度條10
- 2022-09-14 Redis核心原理詳細解說_Redis
- 2022-06-08 CentOs7下docker簡單實踐,安裝nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支