C#学习之泛型准备

想要把泛型搞明白,最好先弄明白下面的代码实例

本实例是建立了两个类,然后在类中可以添加任意类型的值,并且可以利用foreach语句读出

  1 //第一个节点类,放在一个文件中
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 using System.Collections;
  8 
  9 namespace CommonGeneral
 10 {
 11     //编写节点类,类型为object(在c#中所有的类型都是object 类的派生类)
 12     //可以向本类中添加任何类型的值
 13     //本类具有的属性是next,prev,Value
 14     public class LinkNode
 15     {
 16         public LinkNode(object value)
 17         {
 18             this.Value = value;
 19         }
 20 
 21         public object Value
 22         {
 23             get;
 24             private set;//设置成private将不允许在类外用直接向本属性赋值
 25             //只允许在对象的初始化时赋值
 26         }
 27         public LinkNode next
 28         {
 29             get;
 30             internal set;//internal修饰符约定在本程序集内的任何其他函数模块中
 31             //都可以对它赋值。
 32         }
 33         public LinkNode prev
 34         {
 35             get;
 36             internal set;
 37         }
 38     }
 39 }
 40 //第二个类,是对第一个类的调用和存取,建立链表,
 41 //单独放在一个文件家中
 42 using System;
 43 using System.Collections.Generic;
 44 using System.Linq;
 45 using System.Text;
 46 using System.Threading.Tasks;
 47 using System.Collections;
 48 
 49 namespace CommonGeneral
 50 {
 51     //IEnumberable接口是System.Collections命名空间下的一个程序接口
 52     //下面是这个接口的函数
 53     /*
 54          namespace System.Collections
 55          {
 56     // 摘要: 
 57     //     公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。
 58         public interface IEnumerable
 59         {
 60            // 摘要: 
 61         //     返回一个循环访问集合的枚举数。
 62         //
 63         // 返回结果: 
 64         //     一个可用于循环访问集合的 System.Collections.IEnumerator 对象。
 65         [DispId(-4)]
 66         IEnumerator GetEnumerator();
 67        }
 68     }
 69      */
 70     class BigList:IEnumerable
 71     {
 72         public LinkNode Last { get; private set; }
 73         public LinkNode First { get; private set; }
 74         public LinkNode AddLast(object value)
 75         {
 76             var newnode = new LinkNode(value);
 77             if(First==null)
 78             {
 79                 First = newnode;
 80                 Last = newnode;
 81                 Last.next = null;
 82                 Last.prev = null;
 83             }
 84             else
 85             {
 86                 Last.next = newnode;
 87                 newnode.prev = Last;
 88                 Last = newnode;
 89             }
 90             return newnode;
 91         }
 92         public IEnumerator GetEnumerator()
 93         {
 94             LinkNode current = First;
 95             while(current!=null)
 96             {
 97                 yield return current.Value;
 98                 current = current.next;
 99             }
100         }
101        /* IEnumerator IEnumerable.GetEnumberator()
102         {
103             return GetEnumberator();
104         }*/
105     }
106 }
107 
108 //------------主函数---------------
109 using System;
110 using System.Collections.Generic;
111 using System.Linq;
112 using System.Text;
113 using System.Threading.Tasks;
114 
115 namespace CommonGeneral
116 {
117     class Program
118     {
119         static void Main(string[] args)
120         {
121             BigList list = new BigList();
122             list.AddLast(100);
123             list.AddLast("nihao");
124             list.AddLast(true);
125             list.AddLast("Any type value can add into this set");
126             foreach(var value in list)
127             {
128                 Console.WriteLine(value.ToString());
129             }
130             Console.ReadKey();
131             return;
132         }
133     }
134 }

 

posted @ 2014-09-21 09:45  SYTM  阅读(192)  评论(0编辑  收藏  举报