C#---泛型

部分内容由千峰教育(莫新宇)听课笔记总结

泛型Genericity

原始代码

using System;

namespace fTtype
{
    static class MathTool {
        public static void Swap(ref object a, ref object b)
        {
            object temp = a;
            a = b;
            b = temp;
        } 
    }
    class Program
    {
        static void Main(string[] args)
        {
            int num01 = 3;
            int num02 = 5;
            float num03=3.4f;
            float num04 = 5.5f;
            object obj01 = num03;
            object obj02 = num04;
            MathTool.Swap(ref obj01, ref obj02);
            Console.WriteLine(obj01);
            Console.WriteLine(obj02);
        }
    }
}

c#中的反省能够将类型作为参数来产地,即在创建类型时用一个特定的符号,如"T"来作为一个占位符,代替实际的类型,等待在实例化时再用一个实际的类型来代替:

public static void Swap<T>(ref T value0, ref T value1)

{

T temp=value0;

value0=value1;

value1=temp;

}

使用反省可以重用代码,保护类型的安全及提高性能

降低强制转换或装箱炒作的成本和风险

可以对泛型参数进行限定以访问特定数据类型的方法

using System;

namespace fTtype
{
    static class MathTool {
        public static void Swap<T>(ref T a, ref T b) {
            T temp = a;
            a = b;
            b = temp;
        }
        public static void Swap(ref object a, ref object b)
        {
            object temp = a;
            a = b;
            b = temp;
        } 
    }
    class Program
    {
        static void Main(string[] args)
        {
            int num01 = 3;
            int num02 = 5;
            float num03=3.4f;
            float num04 = 5.5f;
            object obj01 = num03;
            object obj02 = num04;
            MathTool.Swap(ref obj01, ref obj02);
            Console.WriteLine(obj01);
            Console.WriteLine(obj02);
            uint score01 = 100;
            uint score02 = 60;
            MathTool.Swap<uint>(ref score01,ref score02);
            Console.WriteLine(score01);
            Console.WriteLine(score02);
        }
    }
}

泛型

有些时候重载的方法只有参数类型不同,其他的都一样,这是就可以使用泛型

泛型:需要用户自己传过来的一个数据类型

平时方法里传递的时参数、变量,参数传递用的是小括号();而泛型传递的是数据类型,用的是<>

泛型方法

泛型方法是使用泛型类型参数声明的方法,当方法中存在某些参数的类型不明确时,就可以使用泛型方法。未知具体类型的参数就使用泛型类型参数替代

访问修饰符 返回值类型 方法名<泛型类型列表>(参数列表)(方法体)

void Test<T,U>(T p1, U p2){   }

泛型在方法哪里使用?

定义参数

方法体内使用泛型定义局部变量

设置返回值类型时一个泛型

泛型参数

泛型类型参数可以有多个

泛型类型参数可以时编译器识别的任何数据类型

泛型类型参数命名需要遵守命名规则

使用描述性名称命名泛型类型,并且使用T作为前缀

单个字母名称完全可以让人了解其表示的含义使用单个大写字母命名

泛型参数约束

方法名(参数列表):where泛型:约束内容

方法名(参数列表):where泛型A:约束内容1,约束内容2where 泛型B:约束内容3

为防止传入的类型导致方法运行出错,可以对泛型进行一定的约束

约束关键词:Where

where T:struct:表示泛型T的值类型(小数、整数、char、bool、struct)

where T:class:表示泛型T是引用类型

where T:new():表示这个泛型具有一个无参数的构造方法,如果有多个约束,new()必须放在最后

where T:基类名:表示这个泛型是该积累或派生类

where T:接口名:表示泛型是实现了该接口的类型

where T:U为T提供的类型参数必须是为U提供的参数或派生自为U提供的参数

using System;

namespace fTtype
{
    static class MathTool {
        public static void Swap<T>(ref T a, ref T b) {
            T temp = a;
            a = b;
            b = temp;
        }
        public static void sSwap<A>(ref A a, ref A b) where A:class,new()  {
            A temp = a;
            a = b;
            b = temp;
        }
        public static void Swap(ref object a, ref object b)
        {
            object temp = a;
            a = b;
            b = temp;
        }
        public static T Print<T>(T msg) {
            T temp = (T)msg;
            Console.WriteLine(msg);
            return temp;
        }
        public static T print<T, U>(T msg, U date) where T:Person where U:Student {
            T temp = (T)msg;
            Console.WriteLine(msg);
            return temp;
        }
    }
    class Student { 
    
    }
    class Person{
        public string name;
        //public Person(string name) { this.name = name; }//new()表示这个泛型具有一个无参数的构造方法
    }

    class Program
    {
        static void Main(string[] args)
        {
            int num01 = 3;
            int num02 = 5;
            float num03=3.4f;
            float num04 = 5.5f;
            object obj01 = num03;
            object obj02 = num04;
            MathTool.Swap(ref obj01, ref obj02);
            Console.WriteLine(obj01);
            Console.WriteLine(obj02);
            uint score01 = 100;
            uint score02 = 60;
            MathTool.Swap<uint>(ref score01,ref score02);
            Console.WriteLine(score01);
            Console.WriteLine(score02);
            Person xiaoming = new Person();
            Person xiaohong = new Person();
            MathTool.Swap<Person>(ref xiaoming,ref xiaohong);
        }
    }
}

泛型方法的泛型重载

如果泛型的个数不同,可以重载

如果泛型的个数相同,但约束不同,不可以重载

using System;

namespace fTtype
{
    static class MathTool {
        public static void Swap<T>(ref T a, ref T b) {
            T temp = a;
            a = b;
            b = temp;
        }
        public static void sSwap<A>(ref A a, ref A b) where A:class,new()  {
            A temp = a;
            a = b;
            b = temp;
        }
        public static void Swap(ref object a, ref object b)
        {
            object temp = a;
            a = b;
            b = temp;
        }
        public static T Print<T>(T msg) {
            T temp = (T)msg;
            Console.WriteLine(msg);
            return temp;
        }
        public static T print<T, U>(T msg, U date) where T:Person where U:Student {
            T temp = (T)msg;
            Console.WriteLine(msg);
            return temp;
        }
    }
    class Student { 
    
    }
    class Person{
        public string name;
        //public Person(string name) { this.name = name; }//new()表示这个泛型具有一个无参数的构造方法
    }
    interface IUSB<T,M> { 
        T usbType { get; set; }
        void ReadFile(T content);
        M WriteFile(M content);
    }
    class Computer<T> : IUSB<int, float>
    {
        public int usbType { get ; set; }

        public void ReadFile(int content)
        {
           
        }

        public float WriteFile(float content)
        {
            throw new NotImplementedException();
        }
    }
    class MiniComputer<T> : Computer<T> {
        T miniComputerName;
        T ShowMiniComputerName(T name) {
            return miniComputerName;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int num01 = 3;
            int num02 = 5;
            float num03=3.4f;
            float num04 = 5.5f;
            object obj01 = num03;
            object obj02 = num04;
            MathTool.Swap(ref obj01, ref obj02);
            Console.WriteLine(obj01);
            Console.WriteLine(obj02);
            uint score01 = 100;
            uint score02 = 60;
            MathTool.Swap<uint>(ref score01,ref score02);
            Console.WriteLine(score01);
            Console.WriteLine(score02);
            Person xiaoming = new Person();
            Person xiaohong = new Person();
            MathTool.Swap<Person>(ref xiaoming,ref xiaohong);
            MiniComputer<string> miniComputer = new MiniComputer<string>();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值