白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DelegateMethod
{
    
internal delegate void DelegateVoidHandler(string a);
    
internal delegate int DelegateFunctionHandler(int a);

    
class Program
    {
        
static void Main(string[] args)
        {
            
int res = 0;
            
//以下两种效果相同
            res = TestAction(13, voidMethod);
            Console.WriteLine(
"指定实体方法传入:" + res.ToString());

            res 
= TestAction(13, i=>{i.ToString();});
            Console.WriteLine(
"指定匿名方法传入:" + res.ToString());

            
//以下五种效果相同
            
//Func<int, int> fun 效果等同于 delegate int DelegateFunctionHandler(int a)
            res = TestDelegate(23, (val) =>
            { 
return val * val; }
                );
            Console.WriteLine(
"指定匿名函数传入:" + res.ToString());

            res 
= TestDelegate(33delegate(int a) { return a * a; });
            Console.WriteLine(res.ToString());

            res 
= TestDelegate(43, functionMethod);
            Console.WriteLine(res.ToString());

            res 
= TestFunc(53, (a) =>
            { 
return a * a; }
                );
            Console.WriteLine(
"指定匿名函数传入:" + res.ToString());

            res 
= TestFunc(63delegate(int a)
            { 
return a * a; }
                );
            Console.WriteLine(
"指定匿名函数传入:" + res.ToString());

            Console.ReadKey();
        }

        
/// <summary>
        
/// 封装方法参数的方法
        
/// </summary>
        
/// <param name="a"></param>
        
/// <param name="b"></param>
        
/// <param name="action"></param>
        
/// <returns></returns>
        private static int TestAction(int a, int b, Action<int> action)
        {
            
int sum = a + b;
            
if (action == nullreturn sum;
            action(sum);
            
return sum;
        }

        
/// <summary>
        
/// 带委托函数的方法
        
/// </summary>
        
/// <param name="a"></param>
        
/// <param name="b"></param>
        
/// <param name="action">委托参数,可以传入匿名函数和实体函数</param>
        
/// <returns></returns>
        private static int TestDelegate(int a, int b, DelegateFunctionHandler action)
        {
            
int sum = a + b;
            
if (action == nullreturn sum;
            sum 
= action(sum);
            
return sum;
        }

        
private static int TestFunc(int a, int b, Func<intint> fun)
        {
            
int sum = a + b;
            
if (fun == nullreturn sum;
            sum 
= fun(sum);
            
return sum;
        }

        
public static void voidMethod(int a)
        {
            a 
= a + 1;
        }

        
private static int functionMethod(int a)
        {
            
return a * a;
        }
    }
}

posted on 2009-08-06 16:26 白开心 阅读(324) 评论(1)  编辑 收藏 引用 所属分类: .Net(学习ing...)

评论

# re: 委托和匿名函数 2013-05-06 01:46 bracelet shopping
感叹,我不知道iBATIS的示例是否真的是这个示例当成了回事,还是因为它上面有一本名为“iBATIS IN ACTION”的书的原因,是希望我们在出问题的时候去购买这本书吗???反正这是被欺负够了,自己又没有这个能耐去写这么一个东东。  回复  更多评论
  

只有注册用户登录后才能发表评论。