c# 委托

用代码来讲话,例如下面是我们写的一段代码:

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

namespace CSharp.Lambda
{
   public class DelegateBefore
    {
        public long Add(int a,int b)
        {
            return a + b;
        }

        public long Multiply(int a, int b)
        {
            return a * b;
        }

        public long GetAdd(int a, int b)
        {
            Console.WriteLine("今天我们要学习委托啦啦乌啦啦~~~~");
            long result = Add(a, b);
            Console.WriteLine(result);
            Console.WriteLine("啦啦乌啦啦~~~~");
            return result;
        }

        public long GetMultiply(int a, int b)
        {
            Console.WriteLine("今天我们要学习委托啦啦乌啦啦~~~~");
            long result=Multiply(a, b);
            Console.WriteLine(result);
            Console.WriteLine("啦啦乌啦啦~~~~");
            return result;
        }
    }
}


可以看出GetAdd和GetMultiply函数大部分重复,又不好提取公共函数,那么这个时候可以用到委托,把函数当作参数来传递。

还是用代码来说话,一目了然,怎么改写代码呢,使用委托后的代码如下:

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

namespace CSharp.Lambda
{
    public class Delegate
    {
        public long Add(int a,int b)
        {
            return a + b;
        }

        public long Multiply(int a, int b)
        {
            return a * b;
        }

        public long GetAdd(int a, int b)
        {
            //第一个前面的参数类型为函数的参数类型
            //最后一个参数类型是返回值的参数类型
            //无返回值不写
            //static类型函数不能委托
            Func<int, int, long> all = new Delegate().Add;
           return GetAll(all, a, b);
        }

        public long GetMultiply(int a, int b)
        {
            Func<int, int, long> all = new Delegate().Multiply;
            return GetAll(all, a, b);
        }

        public long GetAll(Func<int, int, long> all, int a, int b)
        {
            Console.WriteLine("今天我们要学习委托啦啦乌啦啦~~~~");
            long result = all(a, b);
            Console.WriteLine(result);
            Console.WriteLine("啦啦乌啦啦~~~~");
            return result;
        }

    }
}

单元测试一下是否正确:

using CSharp.Lambda;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Test
{
    [TestClass()]
    public class DelegateBeforeTest
    {
        [TestMethod()]
        public void GetMultiplyTest()
        {
            CSharp.Lambda.DelegateBefore target = new CSharp.Lambda.DelegateBefore(); 
            int a = 10; 
            int b = 20;
            long result = target.GetMultiply(a, b);
            Assert.AreEqual(200,result);
        }

        [TestMethod()]
        public void GetAddTest()
        {
            CSharp.Lambda.DelegateBefore target = new CSharp.Lambda.DelegateBefore();
            int a = 10;
            int b = 20;
            long result = target.GetAdd(a, b);
            Assert.AreEqual(30, result);
        }
    }
}
using CSharp.Lambda;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;


namespace Test
{
    [TestClass()]
    public class DelegateTest
    {
        [TestMethod()]
        public void GetMultiplyTest()
        {
            CSharp.Lambda.Delegate target = new CSharp.Lambda.Delegate();
            int a = 10;
            int b = 20;
            long result = target.GetMultiply(a, b);
            Assert.AreEqual(200, result);
        }

        [TestMethod()]
        public void GetAddTest()
        {
            CSharp.Lambda.Delegate target = new CSharp.Lambda.Delegate();
            int a = 10;
            int b = 20;
            long result = target.GetAdd(a, b);
            Assert.AreEqual(30, result);
        }
    }
}

测试正确。


Func<T, TResult> 委托

in T

此委托封装的方法的参数类型。

out TResult

此委托封装的方法的返回值类型。

封装的方法必须与此委托定义的方法签名相对应。也就是说,封装的方法必须具有一个通过值传递给它的参数,并且必须返回值。

posted on 2015-08-10 13:26  爱学习的小灵子  阅读(217)  评论(0编辑  收藏  举报