• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
FtpCsharp
博客园    首页    新随笔    联系   管理    订阅  订阅

Method as a method parameters

delegate using as a method parameter

We all know that a method parameters which always contains value type and reference type.But it is so abstract to transmit a method to method.This paper,I will give the answer.

Delegate:
Delegate is a method pointer.It can constraint one type of method rely on its method parameters and  return type.If you want to make the delegate point to a method.The method must have the same return type and  the same parmaters type which ordered as the delegate parmaters.

 

The follow code show the delegate as a method parameters!

 

using System;
namespace CallBackDelegate
{
    public delegate string ProcessDelegate(string strone);
    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            string firstTest = test.Process("wulong", test.ProcessTestOne);
            string secondTest = test.Process("wulong",test.ProcessTestTwo);
            Console.WriteLine(firstTest);
            Console.WriteLine(secondTest);
            Console.Read();
        }
    }
    public class Test
    {
        public string Process(string strone, ProcessDelegate process)
        {
            return process(strone);
        }
        public string ProcessTestOne(string strone)
        {
            return "The first Test is " + strone;
        }
        public string ProcessTestTwo(string strone)
        {
            return "The second Test is " + strone;
        }
    }
}

posted @ 2010-11-21 16:16  FtpCsharp  阅读(268)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3