天天看點

Lambda中的困惑總結(C#)

最近在學.net core,裡面的Lambda表達式比較讓人困惑,不信嗎?請看下面的代碼:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
           

第一眼,有點暈,是不是覺得像一個字段?其實它是一個靜态的方法,現在看不懂沒關系,下面帶着你使用最簡單的代碼講明白它,如果你看得懂,那麼下面的内容你可以不用看了。

好了,廢話到此結束,下面進入正題

當Lambda表達式、匿名方法和Linq兩兩搭配使用時,有時候我們都會懷疑自己是不是沒學過C#?這是由于我們平時用得比較少。

其實Lambda的本質是匿名方法,Lambda表達式和匿名方法一般都會與委托搭配使用,Lambda使用場景可以總結為下面3點:

(1) 代替方法的大括号{  },特别是在.net core中的源碼中看得比較多,不知是不是寫源碼那個家夥為了裝逼,還是為了提高效率

(2) 充當委托執行個體(相對于一個匿名方法)

(3) 搭配List集合或者數組使用(Linq查詢)

下面以例子的方式進行一一講述

一   當方法體中隻有一句代碼時,可以 代替方法的大括号{  }

public class Test
{
     
        public void TestMethod1(string param) => Console.WriteLine("TestMethod1:" + param);

}


 public class Program
    {
        public static void Main(string[] args)
        {
            Test test = new Test();
            test.TestMethod1("1");
              
        }
       
    }
           

上述代碼輸出:TestMethod1:1

其實上面的Test類中的TestMethod1可以寫成我們經常寫的形式:

public void TestMethod1(string param){

  Console.WriteLine("TestMethod1:" + param);
}
           

二  充當委托執行個體

public class Test
    {
        
        public void TestMethod1(string param)
        {
            //其實下面的(param) => { Console.WriteLine("TestMethod1:" + param); }相當于一個匿名方法,指派給Action委托
            Method((param) => { Console.WriteLine("TestMethod1:" + param); })(param);
        }

        private Action<string> Method(Action<string> action)
        {
            return action;
        }
    }


 public class Program
    {
        public static void Main(string[] args)
        {
            Test test = new Test();
            test.TestMethod1("1");
              
        }
       
    }
           

上述代碼輸出:TestMethod1:1

其實上面的Test類中的TestMethod1可以寫成我們經常寫的形式:

public class Test
    {
        
        public void TestMethod1(string param)
        {
             //匿名方法指派給委托
            Action<string> action = delegate (string str) {
                Console.WriteLine("TestMethod3:" +str);
            };
            Action<string> ReturnAction=Method3(action);
            ReturnAction(param);
        }

        private Action<string> Method(Action<string> action)
        {
            return action;
        }
    }
           

三 搭配List或者數組使用(Linq查詢),下面以數組進行說明:

public class Test
    {
        public void TestMethod1(string param) {
            string[] array = {"1","2","3","4","5","6" };
            //使用Linq的擴充方法查詢數組中與param一樣的元素進行傳回
            var result=array.Where(a => a== param);
            foreach (var item in result)
            {
                Console.WriteLine("TestMethod1:"+item);
            }
        }
    }



public class Program
    {
        public static void Main(string[] args)
        {
            Test test = new Test();
            test.TestMethod1("1");
              
        }
       
    }
           

上述代碼輸出:TestMethod1:1

看到這裡,你是否會還原文章最前面的方法IHostBuilder了,先自己還原一下,再看我還原的:

public static IHostBuilder CreateHostBuilder(string[] args) {
            IHostBuilder bulider = Host.CreateDefaultBuilder(args);
            Action<IWebHostBuilder> action = delegate (IWebHostBuilder webBuilder)
            {
                webBuilder.UseStartup<Startup>();
            };
            return bulider.ConfigureWebHostDefaults(action);
          
        }
           

好了,由于水準有限,如有說得不對的,請多多指教

繼續閱讀