C# 委托 Action Func

Action<T> : 传递参数,只进不出,所以方法为void

Func<in T, out Tv> : 传递参数,有进有出,所以传递的方法要return

 

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace rooxml
{

    public class main
    {
        public delegate void fucA(string s);
        public static void Main ()
        {
            Action<string> ii = (string str) => {
                Console.WriteLine(str);
            };
            new A().bb(ii);

            fucA f = new A().aa;  
            f("777");  //委托直接调用
            //new A ().bb (new A().aa);

            Console.WriteLine(
            new A ().cc ((int i)=>{
                return (i*i).ToString();
            })
            );

        }
    }

    class A{
        public A(){ }

        public void aa(string s){
            Console.WriteLine (s+"666");
        }

        public void bb(Action<string> h){
            h ("555");
        }


        public string cc(Func<int, string> c){
            return c(7);
        }
            
    }
}

 

https://msdn.microsoft.com/zh-cn/library/bb549151.aspx

posted @ 2015-04-29 11:41  Cvjar  阅读(169)  评论(0)    收藏  举报