代码改变世界

Action Func delegate

2012-02-27 11:46  通心菜  阅读(268)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        delegate void D1(string name);
        static Action<string> A1;
        static  Func<string,string> F1;
        

        static void Main(string[] args)
        {
            D1 d1 = new D1(HelloWorld);
            d1("aa");



            D1 d2 = delegate(string name)
            {
                Console.WriteLine("Hello,{0}!", name);
            };
            d2("aaa");


            A1 = new Action<string>(HelloWorld);
            A1("a3");

            F1 = new Func<string,string>(HelloWorld2);
            Console.WriteLine(F1("aaaa"));

            Console.ReadKey();

            
        }

        static void HelloWorld(string name)
        {
            Console.WriteLine("Hello ,{0}!", name);
        }

        static string HelloWorld2(string name)
        {
            return name;
        }
    }

}