[C.C++] Csharp(#)数据类型获取方式

2952 0
黑夜隐士 2022-11-5 09:12:57 | 显示全部楼层 |阅读模式
目录

    C sharp (#) 数据类型获取C#的五大数据类型
      C#类型的派生谱类



C sharp (#) 数据类型获取

这里研究一下关于c#中如何获取变量类型的问题。
首先我们研究一下如何获取单个变量的类型
  1. // 问题一:获取单个变量的类型
  2. // 方法一:使用GetType()方法
  3. public static void JudgeType()
  4. {
  5.   int element = 5;
  6.   // 我们应该知道, GetType()会返回一个类型,因此我们需要用类型变量来存储它
  7.   Type type = element.GetType();
  8.   // 如果我们需要判断这个类型与其他的类型,比如与int类型,那么我们应该与typeof(int)进行比较
  9.   if (type == typeof(int))
  10.   {
  11.     Console.WriteLine("Is the type of element int? {0}", "Yes");
  12.   }
  13. }
  14. // =============================================
  15. // 方法二:使用is方法
  16. public static void JudgeType()
  17. {
  18.   // 这里为了避免warning的出现,我们使用object来定义变量
  19.   object element = 5;
  20.   // 使用is来直接判断变量的类型
  21.   if (element is int)
  22.   {
  23.     Console.WriteLine("Is the type of element int? {0}", "Yes");
  24.   }
  25. }
复制代码
接下来我们研究一下如何获取列表变量的类型
  1. // 问题二: 获取列表的类型
  2. // 方法一:使用GetType()方法
  3. public static void JudgeType()
  4. {
  5.   // 创建一个列表对象
  6.   vr list = new List<int>() { 1, 2 };
  7.   Type type = list.GetType();
  8.   if (type == typeof(List<int>))
  9.   {
  10.     Console.WriteLine("Is the type of list List<int>? {0}", "Yes");
  11.   }
  12. }
  13. // =============================================
  14. // 方法二:使用is方法
  15. public static void JudgeType()
  16. {
  17.   var list = new List<int>() { 1, 2 };
  18.   if (list is List<int>)
  19.   {
  20.     Console.WriteLine("Is the type of list List<int>? {0}", "Yes");
  21.   }
  22. }
  23. // =============================================
  24. // 方法三:使用GetType()和GetGenericArguments()方法
  25. public static void JudgeType()
  26. {
  27.   var list = new List<int>() { 1, 2 };
  28.   Type[] type = list.GetType().GetGenericArguments();
  29.   if (type[0] == typeof(int))
  30.   {
  31.     Console.WriteLine("Is the type of list List<int>? {0}", "Yes");
  32.     Console.WriteLine("Is the type of element in list int? {0}", "Yes");
  33.   }
  34. }
  35. // =============================================
  36. // 方法四: 使用GetType()和ToString()方法
  37. public static void JudgeType()
  38. {
  39.   var list = new List<int>() { 1, 2 };
  40.   foreach (var element in list)
  41.   {
  42.     Type type1 = element.GetType();
  43.     if (type1.ToString() == "System.Int32")
  44.     {
  45.       Console.WriteLine("Is the type of element in list int? {0}", "Yes");
  46.     }
  47.   }
  48. }
  49. // =============================================
  50. // 方法五: 使用GetType()和Name方法
  51. public static void JudgeType()
  52. {
  53.   var list = new List<int>() { 1, 2 };
  54.   string type_ = list[0].GetType().Name;
  55.   Console.WriteLine(type_);
  56.   if (type_ == "Int32")
  57.   {
  58.     Console.WriteLine("Is the type of element in list int? {0}", "Yes");
  59.   }
  60. }
复制代码
C#的五大数据类型

1.类(class):如Windows,Form,Console,String
2.结构体(Structures):如Int32,Int64,Single,Double
3.枚举(Enumerations):如HorizontalAlignment,Visibility
4.接口(Interfaces)
5.委托(Delegates)

C#类型的派生谱类



以上为个人经验,希望能给大家一个参考,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行