C#之泛型使用 IEnumerable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ss
{
    public class LinkListNode  //链表节点
    {
        public object Value;    //节点内容
        public LinkListNode(object node)   //构造函数
        {
            this.Value = node;
        }
        public LinkListNode next;    //指向后继节点
        public LinkListNode prev;   //指向前驱节点
    }
    public class LinkList : IEnumerable
    {
        public LinkListNode first;    //链表的第一个节点指针
        public LinkListNode last;     //链表的最后一个节点指针
        public LinkListNode AddLast(object node)  //添加一个节点
        {
            var newNode = new LinkListNode(node);
            if (first == null)
            {
                first = newNode;
                last = first;
            }
            else
            {
                last.next = newNode;
                newNode.prev = last;
                last = newNode;
            }
            return newNode;
        }
        public IEnumerator GetEnumerator()
        {
            LinkListNode temp = first;
            while (temp != null)
            {
                yield return temp.Value;
                temp = temp.next;
            }
        }
    }
    class Program
    {
        static void Main()
        {
            LinkList list1 = new LinkList();
            list1.AddLast(1);
            list1.AddLast(2);
            list1.AddLast(3);
            foreach (int i in list1)
            {
                Console.WriteLine(i);
            }
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ss
{
    public class LinkListNode<T>
    {
        public T Value;    //节点值
        public LinkListNode<T> next;  //后继节点指针
        public LinkListNode<T> prev;  //前驱节点指针
        public LinkListNode(T value)  //构造函数
        {
            this.Value = value;
        }
    }
    public class LinkList<T> : IEnumerable<T>
    {
        public LinkListNode<T> first;
        public LinkListNode<T> last;
        public LinkListNode<T> AddLast(T node)
        {
            LinkListNode<T> newNode = new LinkListNode<T>(node);
            if (first == null)  //此链表无内容
            {
                first = newNode;
                last = first;
            }
            else
            {
                last.next = newNode;
                newNode.prev = last;
                last = newNode;
            }
            return newNode;
        }
        public IEnumerator<T> GetEnumerator()
        {
            LinkListNode<T> current = first;
            while (current != null)
            {
                yield return current.Value;
                current = current.next;
            }
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    class Program
    {
        static void Main()
        {
            LinkList<int> list1 = new LinkList<int>();
            list1.AddLast(1);
            list1.AddLast(2);
            list1.AddLast(3);
            list1.AddLast(4);
            foreach (int i in list1)
            {
                Console.WriteLine(i);
            }
        }
    }
}
//泛型 约束 默认综合运用
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ss
{
    // *********************************文档定义
    public interface IDocument       //定义一个借口
    {
        string Title { get; set; }
        string Content { get; set; }
    }
    public class Document : IDocument 
    {
        public string Title { get; set; }      //实现借口
        public string Content { get; set; }    //实现借口
        public Document(string title,string content) //构造函数
        {
            this.Title=title;
            this.Content=content;
        }
    }
    //***************************
    public class DocumentManager<T> where T:IDocument     //T为文档类型  并且T必须实现IDocument接口
    {
        private readonly Queue<T> documentQueue = new Queue<T>();   //建立一个泛型队列
        public void AddDocument(T doc)  //添加一个文档
        {
            lock (this)
            {
                documentQueue.Enqueue(doc);
            }
        }
        public bool IsDocuementAvailable   //字段
        {
            get { return documentQueue.Count > 0; }
        }
        public T GetDocument()   //获取文档
        {
            T doc = default(T); //默认值
            lock (this)
            {
                doc = documentQueue.Dequeue();
            }
            return doc;
        }
        public void DisplayAllDocuments()   //展示文档Title属性
        {
            foreach (T doc in documentQueue)
            {
                Console.WriteLine(((IDocument)doc).Title);
            }
        }
    }
    class Program
    {
        static void Main()
        {
            DocumentManager<Document> dm = new DocumentManager<Document>();
            dm.AddDocument(new Document("Title A", "Sample A")); //添加文档
            dm.AddDocument(new Document("Title B", "Sample B"));
            dm.DisplayAllDocuments();  //展示文档
            if (dm.IsDocuementAvailable)
            {
                Document d = dm.GetDocument();  //获取一个文档,即出栈一个元素
                Console.WriteLine(d.Content);
            }
        }
    }
}

泛型接口的协变和抗变

//泛型 约束 默认综合运用
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ss
{
    public class Shape      //基类
    {
        public double width;  //宽
        public double height;  //高
        public override string ToString()
        {
            return String.Format("{0},Height:{1}", width, height);
        }
    }
    public class Rectangle : Shape       //子类
    {
        public double Area()       //计算面积
        {
            return width * height;
        }
    }
    public interface IIndex<out T>       //泛型接口
    {
        T this[int index] { get; }
        int Count { get; }
    }
    public interface IDisplay<in T>
    {
        void Show(T item);
    }
    public class RectangleCollection : IIndex<Rectangle>    //注:协变 子类
    {
        private Rectangle[] data = new Rectangle[3]
        {
            new Rectangle{height=2,width=5},
            new Rectangle{height=3,width=7},
            new Rectangle{height=4.5,width=2.9}
        };
        public static RectangleCollection GetRectangles()
        {
            return new RectangleCollection();
        }
        public Rectangle this[int index]       //实现泛型接口中的内容
        {
            get 
            {
                if (index < 0 || index > data.Length)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                return data[index];
            }
        }
        public int Count         //实现泛型接口中的内容
        {
            get 
            {
                return data.Length;
            }
        }
    }
    public class ShapeDisplay : IDisplay<Shape>    //注:抗变 父类
    {
        public void Show(Shape s)
        {
            Console.WriteLine("{0}{1},Height:{2}", s.GetType().Name, s.width, s.height);
        }
    }
    class Program
    {
        static void Main()
        {
           IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles();
            IIndex<Shape> shapes = rectangles;        //将多种多样的子类赋给父类,通过父类来输出子类的信息
            for (int i = 0; i < shapes.Count; i++)    //通过此种方法可以通过父类来确定是哪个子类
            {
                Console.WriteLine(shapes[i]);
            }
           //IDisplay<Shape> shapeDisplay = new ShapeDisplay();
           //IDisplay<Rectangle> rectangleDisplay = shapeDisplay;
           //rectangleDisplay.Show(rectangles[0]);
        }
    }
}




原文地址:https://www.cnblogs.com/zztong/p/6695178.html