泛型约束
Posted sntetwt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了泛型约束相关的知识,希望对你有一定的参考价值。
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<T>、IComparable<T> //参考:https://msdn.microsoft.com/zh-cn/library/sx2bwtw7.aspx //http://biancheng.dnbcw.info/net/358503.html namespace WpfApplication1 { //这个泛型类只接受值类型的泛型参数 public class MyClass2 { } /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { #region T:结构 /// <summary> /// 调用: /// float _float = StructA<float>(1); //结果:2.0,约束为返回float类型 /// </summary> /// <typeparam name="T">约束T</typeparam> /// <param name="arg">int</param> /// <returns>返回T</returns> private T StructA<T>(int arg) where T : struct { double db = 1.0 + arg; return (T)Convert.ChangeType(db, typeof(T)); } /// <summary> /// 调用: /// int _int = StructB<int>(1); //结果:2,约束为返回int类型 /// </summary> /// <typeparam name="T">约束T</typeparam> /// <param name="arg">任意类型</param> /// <returns>返回T</returns> private T StructB<T>(T arg) where T : struct { double db = 1.0 + (double)Convert.ChangeType(arg, typeof(double)); return (T)Convert.ChangeType(db, typeof(T)); } /// <summary> /// 调用: /// float? y = 1; /// float _float = StructC(y); //结果:2.0,约束为返回float类型 /// </summary> /// <typeparam name="T">约束T</typeparam> /// <param name="arg">任意可空类型</param> /// <returns>返回T</returns> private T StructC<T>(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>(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<string> testa = new InstanceA<string>("hello"); string str = testa.obj; //调用委托 InstanceA<float>.StackDelegate ds = DoWork; InstanceB<类别> testb = new InstanceB<类别>(); //委托 //调用方式一 DelegateT<string> dt = Method; string dts = dt("hello"); //结果:hello //调用方式二 DelegateT<string> d = new DelegateT<string>(Method); dts = dt("hello"); } static string Method(string s) { return s; } delegate string DelegateT<T>(T value); delegate T MyDelegate<T>() where T : new(); private static void DoWork(float[] items) { } //自定义泛型约束 //public bool MyMethod<T>(T t) where T : IMyInterface { } } #region T:new() //<T>为任意类型 public class InstanceA<T> { public T obj{set;get;} public InstanceA(T obj) { this.obj = obj; } //创建泛型实例对像 public static T Instance() { T t = System.Activator.CreateInstance<T>(); return t; } public delegate void StackDelegate(T[] items); } //where T:new()指明了创建T的实例时应该具有构造函数。 //new()约束,要求类型参数必须提供一个无参数的构造函数。 public class InstanceB<T> where T : new() { public static T Instance() { T t = new T(); return t; } } //where T: 类别,T表示类型变量是继承于“类别” public class InstanceC<T> where T : 类别 { } #endregion #region T:class interface 接口<T> where T : class { void Insert(T entity); void Update(T entity); } public class 实现<T> : 接口<T> 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<T> where T : IComparable { } //传人T和U类型,但T类型必须是class类型,U类型必须是struct类型 public class MyClass<T, U>where T : class where U : struct{ } }
以上是关于泛型约束的主要内容,如果未能解决你的问题,请参考以下文章
《C#零基础入门之百识百例》(八十二)泛型类型参数Where约束 -- 泛型单例