C#中的Action<>和Func<>

来源:互联网 发布:编程打印空心字符菱形 编辑:程序博客网 时间:2024/04/26 08:33

其实他们两个都是委托【代理】的简写形式。

一、【action<>】指定那些只有输入参数,没有返回值的委托
Delegate的代码:
[csharp] 
public delegate void myDelegate(string str); 
public static void HellowChinese(string strChinese) 
Console.WriteLine("Good morning," + strChinese);
    Console.ReadLine(); 
 
myDelegate d = new myDelegate(HellowChinese); 
d("Mr wang"); 
用了Action之后呢:
[csharp] 
public static void HellowChinese(string strChinese) 
    Console.WriteLine("Good morning," + strChinese); 
    Console.ReadLine(); 
 
Action<string> action = HellowChinese; 
action("Spring."); 
就是相当于省去了定义委托的步骤了。
二、func<> 这个和上面的那个是一样的,区别是这个有返回值!
[csharp] 
public static string HelloEnglish(string strEnglish) 
    return "Hello." + strEnglish; 
 
Func<string, string> f = HelloEnglish; 
Console.WriteLine(f("Srping ji")); 
Console.ReadLine(); 
 

private void button5_Click(object sender, EventArgs e)
        {
            //类似委托功能
            Func<string, int> test = TsetMothod;

            Console.WriteLine(test("123"));

            Func<string, int> test1 = TsetMothod;


            //只需要调用这个类就可以减少重复的代码
            CallMethod<string>(test1, "123");

            //或者采用这种
            CallMethod<string>(new Func<string, int>(TsetMothod), "123");

            CallMethod(new Func<string, int>(TsetMothod), "123");


        }

public static void CallMethod<T>(Func<T, int> func, T item)
        {
            try
            {
                int i = func(item);
                Console.WriteLine(i);
            }
            catch (Exception e)
            {

            }
            finally
            {

            }
        }

        public static int TsetMothod(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return 1;
            }
            return 0;

        }

----------------------------------Func例-------------------------------------------

 private void button6_Click(object sender, EventArgs e)
        {
                 // Note that each lambda expression has no parameters.
              LazyValue<int> lazyOne = new LazyValue<int>(() => ExpensiveOne());
              LazyValue<long> lazyTwo = new LazyValue<long>(() => ExpensiveTwo("apple"));

              Console.WriteLine("LazyValue objects have been created.");

              // Get the values of the LazyValue objects.
              Console.WriteLine(lazyOne.Value);
              Console.WriteLine(lazyTwo.Value);


        }

         static int ExpensiveOne()
           {
              Console.WriteLine("\nExpensiveOne() is executing.");
              return 1;
           }

       static long ExpensiveTwo(string input)
       {
          Console.WriteLine("\nExpensiveTwo() is executing.");
          return (long)input.Length;
       }

    }

    class LazyValue<T> where T : struct
    {
        private Nullable<T> val;
        private Func<T> getValue;

        // Constructor.
        public LazyValue(Func<T> func)
        {
            val = null;
            getValue = func;
        }

        public T Value
        {
            get
            {
                if (val == null)
                    // Execute the delegate.
                    val = getValue();
                return (T)val;
            }
        }
    }

原创粉丝点击