C#自定义泛型

简介:
using System;
using System.Collections.Generic;
using System.Text;

namespace CustomGenericCollection
{
    #region 汽车的定义
    public class Car
    {
        public string PetName;
        public int Speed;

        public Car(string name, int currentSpeed)
        {
            PetName = name;
            Speed = currentSpeed;
        }
        public Car() { }
    }

    public class SportsCar : Car
    {
        public SportsCar(string p, int s)
            : base(p, s) { }
        // 其他方法
    }

    public class MiniVan : Car
    {
        public MiniVan(string p, int s)
            : base(p, s) { }
        // 其他方法
    }
    #endregion

    #region 自定义泛型集合
    public class CarCollection<T> : IEnumerable<T> where T : Car//:下面的泛型集合类的项目必须是Car 或Car的继承类
    {
        private List<T> arCars = new List<T>();
        public T GetCar(int pos)
        {
            return arCars[pos];
        }
        public void AddCar(T c)
        {
            arCars.Add(c);
        }
        public void ClearCars()
        {
            arCars.Clear();
        }
        public int Count
        {
            get { return arCars.Count; }
        }
        // IEnumerable<T>扩展自IEnumerable的,因此,我们需要实现的GetEnumerator()方法的两个版本。
        System.Collections.Generic.IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return arCars.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return arCars.GetEnumerator();
        }

        public void PrintPetName(int pos)
        {
            Console.WriteLine(arCars[pos].PetName);
        }
    }
    #endregion

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Custom Generic Collection *****\n");
            CarCollection<Car> myCars = new CarCollection<Car>();
            myCars.AddCar(new Car("Rusty", 20));
            myCars.AddCar(new Car("Zippy", 90));

            foreach (Car c in myCars)
            {
                Console.WriteLine("PetName: {0}, Speed: {1}", c.PetName, c.Speed);
            }
            Console.WriteLine();

            // CarCollection<Car> can hold any type deriving from Car.
            CarCollection<Car> myAutos = new CarCollection<Car>();
            myAutos.AddCar(new MiniVan("Family Truckster", 55));
            myAutos.AddCar(new SportsCar("Crusher", 40));
            foreach (Car c in myAutos)
            {
                Console.WriteLine("Type: {0}, PetName: {1}, Speed: {2}",
                  c.GetType().Name, c.PetName, c.Speed);
            }

            Console.ReadLine();
        }
    }
}

目录
相关文章
|
3月前
|
存储 安全 编译器
C# 11.0中的泛型属性:类型安全的新篇章
【1月更文挑战第23天】C# 11.0引入了泛型属性的概念,这一新特性为开发者提供了更高级别的类型安全性和灵活性。本文将详细探讨C# 11.0中泛型属性的工作原理、使用场景以及它们对现有编程模式的改进。通过深入了解泛型属性,开发者将能够编写更加健壮、可维护的代码,并充分利用C#语言的最新发展。
|
6月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
36 0
|
7月前
|
存储 算法 安全
C#三十二 泛型的理解和使用
C#三十二 泛型的理解和使用
21 0
|
29天前
|
C#
C#学习相关系列之自定义遍历器
C#学习相关系列之自定义遍历器
|
1月前
|
存储 安全 Java
34.C#:listT泛型集合
34.C#:listT泛型集合
16 1
|
1月前
|
开发框架 安全 .NET
C# .NET面试系列三:集合、异常、泛型、LINQ、委托、EF!
<h2>集合、异常、泛型、LINQ、委托、EF! #### 1. IList 接口与 List 的区别是什么? IList 接口和 List 类是C#中集合的两个相关但不同的概念。下面是它们的主要区别: <b>IList 接口</b> IList 接口是C#中定义的一个泛型接口,位于 System.Collections 命名空间。它派生自 ICollection 接口,定义了一个可以通过索引访问的有序集合。 ```c# IList 接口包含一系列索引化的属性和方法,允许按索引访问、插入、移除元素等。 由于是接口,它只定义了成员的契约,而不提供具体的实现。类似于 IEnumera
145 2
|
7月前
|
编译器 C#
c# 自定义扩展方法
c# 自定义扩展方法
|
3月前
|
存储 安全 算法
C# 泛型:类型参数化的强大工具
【1月更文挑战第7天】本文将深入探讨C#语言中的泛型编程,包括泛型的定义、用途、优势以及实际应用。通过类型参数化,泛型允许开发者编写更加灵活且可重用的代码,同时提高程序的类型安全性和性能。本文将通过示例代码和详细解释,帮助读者更好地理解泛型在C#中的重要性和实用性。
|
4月前
|
存储 Java 编译器
【从Java转C#】第五章:泛型
【从Java转C#】第五章:泛型
|
6月前
|
编译器 C#
C#中导入其它自定义的命名空间
c#中怎么导入其它自定义的命名空间首先要确保已经导入了想要导入的自定义的命名空间。如上图这时编译器应该会报错,此时就需要手动去添加引用了,cs文件默认没有添加引用,只是加载了想要导入的命名空间,但是没有添加引用,所以需要自己要手动添加引用。切记!然后会有一个对话框选择你想引用的命名空间,点击确定即可。注意:一般而言,C#中如果没有改变那么一般项目的类名都默认是Program,在引用时需要注...
67 1
C#中导入其它自定义的命名空间