C sharp (#) 数据类型获取方式

作者:勤奋的大熊猫 时间:2022-03-29 00:33:10 

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#类型的派生谱类

C sharp (#) 数据类型获取方式

来源:https://blog.csdn.net/u011699626/article/details/109164685

标签:C,sharp,数据类型
0
投稿

猜你喜欢

  • Java PreparedStatement用法详解

    2023-08-08 20:20:51
  • 一文探寻Java装箱和拆箱的奥妙

    2022-08-15 21:41:21
  • Java 比较接口comparable与comparator区别解析

    2022-11-26 20:54:24
  • java easyUI实现自定义网格视图实例代码

    2022-05-16 23:52:54
  • Java多线程定时器Timer原理及实现

    2022-03-03 09:53:51
  • java保证对象在内存中唯一性的实现方法

    2023-11-27 21:30:03
  • 详解java模板和回调机制

    2023-08-13 15:33:46
  • JPA中JpaRepository接口的使用方式

    2022-05-02 05:41:12
  • Spring Cloud Config配置文件使用对称加密的方法

    2021-08-09 08:50:02
  • Java非法字符: ‘\\ufeff‘问题及说明

    2023-02-01 09:06:07
  • java实现归并排序算法

    2023-02-09 07:34:01
  • 详细了解C语言二叉树的建立与遍历

    2021-08-17 10:24:01
  • Android自定义控件实现带数值和动画的圆形进度条

    2021-09-09 22:02:46
  • Android输入法弹出时覆盖输入框问题的解决方法

    2023-02-06 06:36:52
  • 一文教你如何使用Databinding写一个关注功能

    2023-09-17 12:26:47
  • 基于jdk动态代理和cglib动态代理实现及区别说明

    2022-04-11 00:32:44
  • 深入理解Java设计模式之抽象工厂模式

    2023-11-28 12:11:34
  • SpringBoot过滤器与拦截器使用方法深入分析

    2023-08-18 10:20:03
  • 属于自己的Android对话框(Dialog)自定义集合

    2022-08-03 23:09:21
  • Mybatis配置之typeAlias标签的用法

    2023-11-27 20:18:20
  • asp之家 软件编程 m.aspxhome.com