zoukankan      html  css  js  c++  java
  • C#泛型。

    作用:

    使用泛型可以实现算法重用。 

     class Program
        {      
            static void Main(string[] args)
            {
                MyClass<string> myClass = new MyClass<string> ;
    
            }    
        }
        //泛型类
        class MyClass<H>
        {
            public void Say(H arg)
            {
                Console.WriteLine(arg);
            }
        }
        //泛型方法
        public class Class1
        {
            public void Say<H>(H msg)
            {
                Console.WriteLine(msg);
            }
        }
        //泛型接口
        public interface IInterface<H>
        {
            H Say();
            void Say1(H msg);
        }
        //实现泛型接口方法一,普通类实现泛型接口。
        public class Class2 : IInterface<string>
        {
            public string Say()
            {
                throw new NotImplementedException();
            }
    
            public void Say1(string msg)
            {
                throw new NotImplementedException();
            }
        }
        //方法二,泛型类实现泛型接口。
        public class Class3<U> : IInterface<U>
        {
            public U Say()
            {
                throw new NotImplementedException();
            }
    
            public void Say1(U msg)
            {
                throw new NotImplementedException();
            }
        }

     泛型约束:

     Myclass<T> where T : struct  必须值类型 / :class 必须引用类型

    多个:

    Myclass<T,K,W,R,P> 

     where T: struct

     where K :class

    where W : 接口   必须实现某个接口

    where R:K    R必须是K类型或K的之类。

    where P : class,new()    必须引用  且带无参构造函数。   逗号隔开。  new必须写在最后。

  • 相关阅读:
    多个DataTable的合并成一个新表
    into #临时表的用法
    触发器获取更新前的数据
    C# 多个CSV文件合并成一个文件
    group by 字段名 with ROLLUP
    删除DataTable重复列,只针对删除其中的一列重复的行(转)
    动态注册jS
    JS 验证
    导出Excel
    C# 导入多个工作薄文件
  • 原文地址:https://www.cnblogs.com/zhangyuhao/p/10531077.html
Copyright © 2011-2022 走看看