c#中泛型方法的声明及定义实例(2)

73 篇文章 3 订阅
46 篇文章 0 订阅

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LambdaTest
{
    public class LambdaCreate
    {
        //自定义委托(Action和Func实质上是封装好的委托)
        //将委托封装成事件来注册,则可以一个事件注册多个同类型方法(事件执行将调用全部已注册过的方法)
        delegate void delegate_action1();
        event delegate_action1 delegate_action1_event;

        delegate void delegate_action2(object a,object b);
        event delegate_action2 delegate_action2_event;

        delegate object delegate_func1(object a);
        event delegate_func1 delegate_func1_event;

        delegate object delegate_func2(object a,object b);
        event delegate_func2 delegate_func2_event;

        //Action和Func实质上是封装好的委托
        //自定义泛型委托(多写几个重载的话可替代Action和Func)
        delegate void delegate_func3<T>(T a, T b);
        delegate T delegate_func4<T>(T a, T b);
        public void Test()
        {
            //除事件外,被定义封装的委托可当成参数传递使用
            //有16个函数重载,最多的有16个形参
            Action action1 = () => { int c = 8 + 9; };
            Action<int, int> action2 = (int a, int b) => { int c = a + b; };
            Action<object, object> action3 = (object a, object b) => { a = b; };
            //有16个函数重载,最多的有16个形参
            Func<int> func1 = () => { int c = 1; return c; };//Fun至少要有一个返回参数,输入形参可没有
            Func<int, int> func2 = (int a) => { int c = a; return c; };
            Func<object, object> func3 = (object a) => { object c = a; return c; };//最后一个形参是返回类型
            //自定义委托,可替代Action和Func(Action和Func实质上是封装好的委托)
            delegate_action1_event += new delegate_action1(() => { int c = 8 + 9; });   
            delegate_action2_event += new delegate_action2((object a,object b) => { object c = a; c = b; });
            delegate_func1_event += new delegate_func1((object a) => { object c = 9;return c; });
            delegate_func2_event += new delegate_func2((object a,object b) => { object c = a; return c; });
            //以上部分方法的lambda表达式写法
            action1 = new Action(() => { int c = 8 + 9; });
            action3 = new Action<object,object>((object a, object b) => { a = b; });
            func1 = new Func<int>(()=> { int c = 1; return c; })  ;
            func3 = new Func<object, object>((object a) => { object c = a; return c; });

            delegate_action1 normal_delegate_action1 = new delegate_action1(() => { int a = 9; });
            delegate_action2 normal_delegate_action2 = new delegate_action2((object a,object b) => {  a = b; });
            delegate_func1 normal_delegate_func1 = new delegate_func1((object a) => { object c = a; return c; });
            delegate_func2 normal_delegate_func2 = new delegate_func2((object a,object b) => { object c = a; return b; });

            normal_delegate_action1 = () => { int a = 9; };
            normal_delegate_action2 = (object a, object b) => { a = b; };
            normal_delegate_func1 = (object a) => { object c = a; return c; };
            normal_delegate_func2 = (object a, object b) => { object c = a; return b; };

            delegate_action1_event += () => { int c = 8 + 9; };
            delegate_action2_event += (object a, object b) => { object c = a; c = b; };
            delegate_func1_event += (object a) => { object c = 9; return c; };
            delegate_func2_event += (object a, object b) => { object c = a; return c; };
            //泛型声明的时候是T,定义实例的时候需要填具体的参数类型(而不是T或T1,T2)
            delegate_func3<int> normal_delegate_func3 = new delegate_func3<int>((int a,int b) => { int c = a + b; });
            delegate_func4<object> normal_delegate_func4 = new delegate_func4<object>((object a, object b) => { object c = a; return b; });
        }
        //************************************ 泛型方法声明定义及使用     
        public  T GetForm<T>(string formName) where T : Form
        {
            T res = default(T);
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name == formName)
                {
                    form.Invoke(new Action(() =>
                    {
                        res = form as T;
                    }));
                    break;
                }
            }
            return res;
        }
        public T GetForm<T,T1,T2,T3>(string formName,T1 t1,T2 t2,T3 t3) where T :Form 
        {
            T res = default(T);
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name == formName)
                {
                    form.Invoke(new Action(() =>
                    {
                        res = form as T;
                    }));
                    break;
                }
            }
            object a1 = t1 as object;
            object a2 = t2 as object;
            object a3 = t3 as object;
            string a4 = t1 as string;
            Form a5 = t2 as Form;
            int a6 = Convert.ToInt32(t3);
            return res;
        }
        public T GetValue<T,T1,T2>(T1 t1,T2 t2) where T:new()
        {
            double res = 0;
            res += Convert.ToInt32(t1);
            res += Convert.ToInt32(t2);
            T t = new T();
            return t;
        }
        public T GetValue<T>(T a1,T a2) where T :class, new()
        {
           
           double res = Convert.ToInt32(a1) + Convert.ToInt32(a2);
            T resValue = res as T;
            return resValue;
        }
        public double GetValue2<T>(T a1, T a2) 
        {

            double res = Convert.ToInt32(a1) + Convert.ToInt32(a2);
           
            return res ;
        }
        public void Test2()
        {
            Form form1 = this.GetForm<Form>("MainForm");
            Form form2 = this.GetForm<Form,object,int,double>("MainForm",new object(),int.MaxValue,double.MaxValue);
            object c = GetValue<object, double, int>( 8, 9);
            object res=GetValue<object>(2,3);
            double res2 = GetValue2<double>(2.0, 3.0);
            double res3 = GetValue2<int>(2, 3);
        }
       


    }


}

 

泛型方法比一般方法代码重用性高且简洁,因为传入参数是动态的,使用时传入类型即可。

下面为定义泛型方法例子:
public T GetInfo<T>(string s) where T:Test,new()
        {
            T l=new T();
            return l;
        }

其中where子句为可选约束语句,T:后面的Test为指定类型,new ():函数主体要使用T类型实例化时必须加此参数,否则编译不通过
使用泛型方法:

Test t = GetInfo<Test>("teststr");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值