using System;using System.Collections.Generic;using System.IO;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;//泛型命名空间: System.Collections.Generic//如: List、IComparable //参考:https://msdn.microsoft.com/zh-cn/library/sx2bwtw7.aspx//http://biancheng.dnbcw.info/net/358503.htmlnamespace WpfApplication1{ //这个泛型类只接受值类型的泛型参数 public class MyClass2 { } /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { #region T:结构 ////// 调用: /// float _float = StructA ///(1); //结果:2.0,约束为返回float类型 /// 约束T /// int ///返回T private T StructA(int arg) where T : struct { double db = 1.0 + arg; return (T)Convert.ChangeType(db, typeof(T)); } /// /// 调用: /// int _int = StructB (1); //结果:2,约束为返回int类型 /// ///约束T /// 任意类型 ///返回T private T StructB(T arg) where T : struct { double db = 1.0 + (double)Convert.ChangeType(arg, typeof(double)); return (T)Convert.ChangeType(db, typeof(T)); } /// /// 调用: /// float? y = 1; /// float _float = StructC(y); //结果:2.0,约束为返回float类型 /// ///约束T /// 任意可空类型 ///返回T private T StructC(T? arg) where T : struct { double db = 1.0 + (double)Convert.ChangeType(arg, typeof(double)); return (T)Convert.ChangeType(db, typeof(T)); } #endregion private T Foo (T arg) where T : class { double db = 1.0 + (double)Convert.ChangeType(arg, typeof(double)); return (T)Convert.ChangeType(db, typeof(T)); } public MainWindow() { InitializeComponent(); /*泛型模板实例化*/ //T:class 实现 <类别> gci = new 实现 <类别> (); gci.Insert(new 类别() { _int = 1, _float = 2, _double = 3.0 }); //T:new() InstanceA testa = new InstanceA 类别> 类别>("hello"); string str = testa.obj; //调用委托 InstanceA .StackDelegate ds = DoWork; InstanceB <类别> testb = new InstanceB <类别> (); //委托 //调用方式一 DelegateT dt = Method; string dts = dt("hello"); //结果:hello //调用方式二 DelegateT 类别> 类别>d = new DelegateT (Method); dts = dt("hello"); } static string Method(string s) { return s; } delegate string DelegateT (T value); delegate T MyDelegate () where T : new(); private static void DoWork(float[] items) { } //自定义泛型约束 //public bool MyMethod (T t) where T : IMyInterface { } } #region T:new() // 为任意类型 public class InstanceA { public T obj{set;get;} public InstanceA(T obj) { this.obj = obj; } //创建泛型实例对像 public static T Instance() { T t = System.Activator.CreateInstance (); return t; } public delegate void StackDelegate(T[] items); } //where T:new()指明了创建T的实例时应该具有构造函数。 //new()约束,要求类型参数必须提供一个无参数的构造函数。 public class InstanceB where T : new() { public static T Instance() { T t = new T(); return t; } } //where T: 类别,T表示类型变量是继承于“类别” public class InstanceC where T : 类别 { } #endregion #region T:class interface 接口 where T : class { void Insert(T entity); void Update(T entity); } public class 实现 : 接口 where T : class { public void Insert(T entity) { } public void Update(T entity) { } } public class 类别 { public 类别() { } public int _int { set; get; } public float _float { set; get; } public double _double { set; get; } } #endregion // 传人T类型,但T类型必须是IComparable类型 public class GenericClass where T : IComparable { } //传人T和U类型,但T类型必须是class类型,U类型必须是struct类型 public class MyClass where T : class where U : struct{ } }