swollaws
漂泊中体会不到生活的味道,那是因为吃不到老妈烧的饭。

      泛型类的定义类似于一般的类,只是要使用泛型类型声明。之后就可以在类中把泛型类型用作成员字段,或方法的参数类型。在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制。如果客户端代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误。这些限制称为约束。约束是使用 where 关键字指定的。

约束

说明

T:结构

类型参数必须是值类型。可以指定除 Nullable 以外的任何值类型。

T:类

类型参数必须是引用类型;这一点也适用于任何类、接口、委托或数组类型。

T:new()

类型参数必须具有无参数的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。

T:<基类名>

类型参数必须是指定的基类或派生自指定的基类。

T:<接口名称>

类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。

T:U

为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束。

      简单约束说明实例:
  1.     //值类型struct|引用类型class约束
  2.     public struct Text
  3.     {
  4.  
  5.     }
  6.  
  7.     public class Test<T>
  8.         where T : struct
  9.     {
  10.         //T在这里是一个值类型
  11.     }
  12.  
  13.     public class Check
  14.     {
  15.         Test<Text> test = new Test<Text>();
  16.     }
  17.  
  18.     //new()约束
  19.     public class Text
  20.     {
  21.         public Text() { }//无参数的构造函数
  22.     }
  23.  
  24.     public class Test<T>
  25.         where T : new()
  26.     {
  27.         //可以在其中使用T t = new T();
  28.     }
  29.  
  30.     public class Check
  31.     {
  32.         Test<Text> test = new Test<Text>();
  33.     }
  34.  
  35.     //基类约束
  36.     public class Text
  37.     {
  38.         public void Add() { }
  39.     }
  40.  
  41.     public class Test<T>
  42.         where T : Text//T继承至Text
  43.     {
  44.         //可以在类型为T的变量上调用Add()方法
  45.     }
  46.  
  47.     //接口约束见下面实例
       自定义泛型实例:
  1. //源代码下载路径:http://media.wiley.com/product_ancillary/41/07645753/DOWNLOAD/Ch10.zip
  2. namespace Wrox.ProCSharp.Generics
  3. {
  4.     using System;
  5.     using System.Threading;
  6.     using System.Collections.Generic;
  7.  
  8.     public interface IDocument
  9.     {
  10.         string Title { get;}
  11.         string Content { get;}
  12.     }
  13.  
  14.     public class Document : IDocument
  15.     {
  16.         private string title;
  17.         public string Title
  18.         {
  19.             get { return title; }
  20.         }
  21.  
  22.         private string content;
  23.         public string Content
  24.         {
  25.             get { return content; }
  26.         }
  27.  
  28.         public Document(string title, string content)
  29.         {
  30.             this.title = title;
  31.             this.content = content;
  32.         }
  33.     }
  34.  
  35.     /// <summary>
  36.     /// 定制泛型类【使用接口约束】
  37.     /// </summary>
  38.     public class ProcessDocuments<TDocument, TDocumentManager>
  39.         where TDocument : IDocument
  40.         where TDocumentManager : IDocumentManager<TDocument>
  41.     {
  42.         public static void Start(TDocumentManager dm)
  43.         {
  44.             new Thread(new ProcessDocuments<TDocument, TDocumentManager>(dm).Run).Start();
  45.         }
  46.  
  47.         protected ProcessDocuments(TDocumentManager dm)
  48.         {
  49.             //注意不能把null赋予泛型类型,因为泛型类型也可以是值类型。
  50.             //其中T doc = default(T);//则会将null赋予引用类型,把0赋予值类型。
  51.             documentManager = dm;
  52.         }
  53.  
  54.         //使用泛型类型定义成员字段
  55.         private TDocumentManager documentManager;
  56.  
  57.         protected void Run()
  58.         {
  59.             while (true)
  60.             {
  61.                 if (documentManager.IsDocumentAvailable)
  62.                 {
  63.                     TDocument doc = documentManager.GetDocument();
  64.                     Console.WriteLine("Processing document {0}", doc.Title);
  65.                 }
  66.                 Thread.Sleep(20);
  67.             }
  68.         }
  69.     }
  70.  
  71.     /// <summary>
  72.     /// 定制泛型接口
  73.     /// </summary>
  74.     public interface IDocumentManager<T>
  75.     {
  76.         void AddDocument(T doc);
  77.         T GetDocument();
  78.         bool IsDocumentAvailable
  79.         {
  80.             get;
  81.         }
  82.     }
  83.  
  84.     /// <summary>
  85.     /// 定制泛型类
  86.     /// </summary>
  87.     public class DocumentManager<T> : IDocumentManager<T>
  88.     {
  89.         private Queue<T> documentQueue = new Queue<T>();
  90.  
  91.         //泛型类型作为参数类型
  92.         public void AddDocument(T doc)
  93.         {
  94.             lock (this)
  95.             {
  96.                 documentQueue.Enqueue(doc);
  97.             }
  98.         }
  99.  
  100.         //泛型类型作为返回类型
  101.         public T GetDocument()
  102.         {
  103.             T doc;
  104.             lock (this)
  105.             {
  106.                 doc = documentQueue.Dequeue();
  107.             }
  108.             return doc;
  109.         }
  110.  
  111.         public bool IsDocumentAvailable
  112.         {
  113.             get { return (documentQueue.Count > 0) ? true : false; }
  114.         }
  115.     }
  116.  
  117.     class Program
  118.     {
  119.         static void Main(string[] args)
  120.         {
  121.             DocumentManager<Document> dm = new DocumentManager<Document>();
  122.  
  123.             ProcessDocuments<Document, DocumentManager<Document>>.Start(dm);
  124.  
  125.             for (int i = 0; i < 1000; i++)
  126.             {
  127.                 Document doc = new Document("title" + i.ToString(), "content");
  128.                 dm.AddDocument(doc);
  129.                 Console.WriteLine("added document {0}", doc.Title);
  130.                 Thread.Sleep(10);
  131.             }
  132.         }
  133.     }
  134. }

 

posted on 2009-05-12 17:42  swollaw  阅读(874)  评论(4编辑  收藏  举报