当前位置: 首页 > news >正文

C# 泛型

概念

泛型实现了类型参数化,达到代码重用目的
通过类型参数化来实现同一份代码上操作多种类型

泛型相当于类型占位符
定义类或方法时使用替代符代表变量类型


当真正使用类或者方法时再具体指定类型

泛型分类

泛型类和泛型接口
   基本语法:
  class 类名<泛型占位字母>


  interface 接口名<泛型占位字母>

泛型函数
    基本语法:函数名<泛型占位字母>(参数列表)

    注意:泛型占位字母可以有多个,用逗号分开

泛型类和接口

class TestClass<T>
{
    public T value;
}    

class TestClass2<T1,T2,K,M,LL,Key,Value>
{
    public T1 value1;
    public T2 value2;
    public K value3;
    public M value4;
    public LL value5;
    public Key value6;
    public Value value7;
}

interface TestInterface<T>
{
    T Value
    {
        get;
        set;
    }
}

class Test : TestInterface<int>   这里要指定接口泛型T的类型
{
    public int Value { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}

泛型方法

1.普通类中的泛型方法

    class Test2
    {
        public void TestFun<T>( T value)
        {
            Console.WriteLine(value);
        }

        public void TestFun<T>()
        {
            //用泛型类型 在里面做一些逻辑处理
            T t = default(T);     //default(类型);返回改类型默认值。
        }

        public T TestFun<T>(string v)
        {
            return default(T);
        }

        public void TestFun<T,K,M>(T t, K k, M m)
        {

        }
    }

    2.泛型类中的泛型方法
    class Test2<T>
    {
        public T value;

        public void TestFun<K>(K k)
        {
            Console.WriteLine(k);
        }

        //  ↓这个不叫泛型方法 因为 T是泛型类申明的时候 就指定 在使用这个函数的时候 
        //我们不能再去动态的变化了
        public void TestFun(T t)
        {

        }
    }

泛型的作用

1.不同类型对象的相同逻辑处理就可以选择泛型
2.使用泛型可以一定程度避免装箱拆箱
   举例:优化ArrayList

class ArrayList<T>
{
    private T[] array;

    public void Add(T value)
    {

    }

    public void Remove( T value)
    {

    }
}


using System.Collections;class Judge
{public static string  Type <T> (){string a;if (typeof(T) == typeof(int)){return a = "整形" + "," + sizeof(int) + "字节";}else if (typeof(T) == typeof(Char)){return a = "字符" + "," + sizeof(Char) + "字节";}else if (typeof(T) == typeof(float)){return a = "浮点数" + "," + sizeof(float) + "字节";}else if (typeof(T) == typeof(String)){return a = "字符串" + "," + "?"+ "字节";}else { return a = "其他类型"; }}}class Program
{public   static void Main(){Console.WriteLine ( Judge.Type<int>());Console.WriteLine(Judge.Type<float >());Console.WriteLine(Judge.Type<double >());Console.WriteLine(Judge.Type<string >());Console.WriteLine(Judge.Type<Char>());}}


http://www.mrgr.cn/news/5515.html

相关文章:

  • 理解现代前端开发:从HTML到React的进化之路
  • 浅谈Spring Boot
  • Python 如何使用 functools 模块
  • Go RPC 和 gRPC 技术详解
  • 学习Math.random()的应用
  • 前端html+js实现懒加载的两种常见方法
  • 一文弄懂 LLM 结构化数据生成原理
  • 基于Python自动连接汕头大学校园网
  • Axios介绍;前后端分离开发的介绍;YAPI的使用;Vue项目简介、入门;Elementui的使用;nginx介绍
  • 支付宝开放平台-开发者社区——AI 日报「8 月23 日」
  • BaseCTF WEEK1 re复现-入土为安的第24天
  • halcon1
  • [Linux#40][线程] 线程控制 | 多线程
  • 设计模式六大原则:迪米特法则详细说明和案例示范
  • windows docker 执行apt-get 权限问题
  • 大数据-95 Spark 集群 SparkSQL Action与Transformation操作 详细解释与测试案例
  • Vue3 provide(父) + inject(子、子的子...)进行值的传递及显示
  • iOS 开发:Object-C 和 Swift 的区别 (AI问答)
  • 三种方法加密图纸!2024如何对CAD图纸进行加密?分享给你
  • 回归预测|基于NGO-TCN-BiGRU-Attention的数据预测Matlab程序 多特征输入单输出 含基础模型