C#的泛型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Apple apple = new Apple() { Color = "red"};
            Book book = new Book() { Name = "Math" };
            Box<Apple> box1 = new Box<Apple>() { Cargo = apple };
            Box<Book> box2 = new Box<Book>() { Cargo = book };
            Console.WriteLine(box1.Cargo.Color);
            Console.WriteLine(box2.Cargo.Name);
        }
    }


    class Apple
    {
        public string Color { get; set; }
    }

    class Book
    {
        public string Name { get; set; }
    }


    class Box<TCargo>   //尖括号里的内容为类型参数,可以随意取名
    {
        public TCargo Cargo { get; set; }
    }


}

这里定义了两个商品类,Apple类和Book类,然后定义了一个Box类,用来装两个商品。这里Box后面接了泛型,尖括号里的内容为类型参数,可以随意取名。只需在使用时对应相应的类,TCargo就会变成相应的类。如定义box1,使用了Apple类,即表示box1为装苹果的箱子,Box类里的属性也为对应的Apple类型。

 

扩展:泛型接口的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TInterfaceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Student<int> stu = new Student<int>();
            stu.ID = 101;
            stu.Name= "Alice";
        }
    }


    interface IUnique<TId>
    {
        TId ID { get; set; }
    }


    class Student<TId> : IUnique<TId>
    {
        public TId ID { get; set; }
        public string Name { get; set; }
    }
}

这里定义了一个泛型接口IUnique,Student类调用该接口,那么Student类也必须是泛型。如果接口类型已经定义好了,那么Student类可以不为泛型,如下代码:

 class Student: IUnique<int>
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

 

 

 

 

泛型方法的使用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TFunctionSample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a1 = {1,2,3,4,5 };
            int[] a2 = { 1, 2, 3, 4, 5, 6 };

            double[] b1 = {1.1, 2.2, 3.3, 4.4, 5.5 };
            double[] b2 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };

            var result = Zip(a1, a2);
            var result2 = Zip(b1, b2);
            foreach (var n in result)
            {
                Console.WriteLine(n); ;
            }

            foreach (var n in result2)
            {
                Console.WriteLine(n); ;
            }
        }


        static T[] Zip<T>(T[] a,T[] b)
        {
            T[] zipped = new T[a.Length + b.Length];
            int ai = 0, bi = 0, zi = 0;
            do
            {
                if (ai < a.Length) zipped[zi++] = a[ai++];
                if (bi < b.Length) zipped[zi++] = b[bi++];
            } while (ai<a.Length||bi<b.Length);
            return zipped;
        }
    }
}

定义泛型方法和之前的定义类似,在函数名后面的尖括号里填写类型参数。不过这里使用的时候不需要显示声明类型参数类型,系统会自动根据传入的参数类型进行转换。例如:result中Zip函数传入的是两个int类型数组,所以T类型自动转换为int类型,无需显示声明。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值