/* 什么是泛型? 如何使用和定义泛型? 泛型是具有占位符(类型参数)的类、结构、接口和方法, 这些占位符是类、结构、接口和方法所存储或使用的一个或多个类型的占位符。 泛型集合类可以将类型参数用作它所存储的对象的类型的占位符; 类型参数作为其字段的类型和其方法的参数类型出现。 泛型方法可以将其类型参数用作其返回值的类型或者其形参的类型之一。 摘自《Microsoft .NET Framework SDK v2.0 文档》 泛型概述 ms-help://MS.NETFramework.v20.chs/dv_fxfund/html/c92e7fb8-bc97-4a06-88f9-2066c58431fa.htm http://msdn.microsoft.com/zh-cn/library/ms172193(VS.95).aspx */ namespace ConsoleApplication { using System; using Microshaoft; public class Class1 { static void Main(string[] args) { //GenericClass<int, string> x = new GenericClass<int, string>(); GenericClass<int, string, Test> x = new GenericClass<int, string, Test>(); x.Add<string>(1, "a"); x.Add<int>(11, "b"); x.Print<int>(); x.Print(new Test("aaaaaaaaaaaaaaaaaaa")); Console.WriteLine("Hello World"); Console.WriteLine(Environment.Version.ToString()); } } class Test : ITest { private string _m = ""; public Test (string x) { _m = x; } public void Aa() { Console.WriteLine(_m); } } } namespace Microshaoft { using System; using System.Collections.Generic; public class GenericClass<T1, T2, T5> where T5 : ITest where T2 : class { private Dictionary<T1, T2> _dictionary = new Dictionary<T1, T2>(); public void Add<T3>(T1 x,T2 y) { T2 z = null; z = default(T2); this._dictionary.Add(x, y); } public void Print<T4>() { foreach(T1 var in this._dictionary.Keys) { Console.WriteLine("{0},{1}", var, _dictionary[var]); } } public void Print(T5 x) { x.Aa(); } } public interface ITest { void Aa(); } } // CS0403.cs // CS0304.cs // compile with: /target:library class C<T> where T : new () { public void f() { //T t = null; // CS0403 T t2 = default(T); // OK t2 = new T(); } } class D<T> where T : class , new () { public void f() { T t = null; // OK t = new T(); } }