泛型约束

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{ }    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值