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);
          
        }

 

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章