zoukankan      html  css  js  c++  java
  • Action C#

    C#中的Action<>和Func<>

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

     
    一、【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();  
  • 相关阅读:
    android progressbar 水平进度条
    jquery 下拉自动加载
    jquery ajax
    input 数字,字母汉字的限制方法(转帖)
    Jquery checkbox
    js运用6
    js运用5
    js运用4
    js运用3
    js运用2
  • 原文地址:https://www.cnblogs.com/sfbrzkh/p/3022625.html
Copyright © 2011-2022 走看看