EF&LINQ開發實戰 Part1

EF&LINQ開發實戰 Part1

在C#3.0將匿名方法以Lambda方式精簡呈現後,說實在的真的看不懂語法,趁這次修練好好徹底弄懂..因此寫了這篇演進修練文:

程式的結果要將委派的方法傳回的字串接收然後印出:

一般寫法,透過三個步驟印出結果:


using System.Collections.Generic;
using System.Linq;
using System.Text;
    class Program
    {
        public delegate string FirstDelegate(string UserName);
        //步驟一:宣告一個委派方法:
        static void Main(string[] args)
        {
            //步驟三:New FirstDelegate 並告知將委派方法(ShowMessage)
            FirstDelegate FD = new FirstDelegate(ShowMessage);
            string message = FD("Kiwi");
            Console.WriteLine(message);
            Console.ReadKey();
        }
        //步驟二:宣告一個靜態委派方法
        //(委派方法的參數個數、順序、型態乃至於回傳型態都必須跟要被委派的方法一樣 (但變數名稱可以不相同)。)
        public static string ShowMessage(string MyName)
        {
            return  MyName + "'s FirstDelegate";
        }         
    }

以上方法就是一般的寫法…

縮減演進(1)..將委派方法拿掉,直接建立使用(沒有NEW的動作)


using System.Collections.Generic;
using System.Linq;
using System.Text;
    class Program
    {
        public delegate string FirstDelegate(string UserName);
        //步驟一:宣告一個委派方法:
        static void Main(string[] args)
        {
            //步驟二:New FirstDelegate ;
            FirstDelegate FD =  delegate (string MyName)
            {
               return  MyName + "'s FirstDelegate";
            };
           Console.WriteLine(FD("Kiwi"));
           Console.ReadKey();
        }       
    }

縮減演進(2-1),把delegate拿掉用=> 取代


using System.Collections.Generic;
using System.Linq;
using System.Text;
    class Program
    {
        public delegate string FirstDelegate(string UserName);
        //步驟一:宣告一個委派方法:
        static void Main(string[] args)
        {
            //步驟二:New FirstDelegate 
            FirstDelegate FD = (string MyName)=>
             {
               return  MyName + "'s FirstDelegate";
            };
           Console.WriteLine(FD("Kiwi"));
           Console.ReadKey();
        }       
    }

縮減演進(2-2),沒有傳入參數時的寫法


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    class Program
    {
        public delegate string FirstDelegate();
        //步驟一:宣告一個委派方法:
        static void Main(string[] args)
        {
            //步驟二:New FirstDelegate
            FirstDelegate FD = ()=>
             {
               return   "FirstDelegate";
            };
           Console.WriteLine(FD());
           Console.ReadKey();
        }       
    }

縮減演進(3),不用定義型別(string MyName) :


using System.Collections.Generic;
using System.Linq;
using System.Text;
    class Program
    {
        public delegate string FirstDelegate(string UserName);
        //步驟一:宣告一個委派方法:
        static void Main(string[] args)
        {
            //步驟二:New FirstDelegate ;並告知將委派方法(ShowMessage)
            FirstDelegate FD = MyName=>
            {
               return  MyName + "'s FirstDelegate";
            };
           Console.WriteLine(FD("Kiwi"));
           Console.ReadKey();
        }       
    }

縮減演進(4),不用宣告delegate ,以 Func<string,string> FD  取代

  // public delegate string FirstDelegate(string UserName);


using System.Collections.Generic;
using System.Linq;
using System.Text;
    class Program
    {
       // public delegate string FirstDelegate(string UserName);
        //步驟一:宣告一個委派方法:
        static void Main(string[] args)
        {
            //步驟一: Func<string,string>以範例第一個string是傳入參數型態,第二個是傳出結果型態
             Func<string,string> FD = MyName=>
            {
               return  MyName + "'s FirstDelegate";
            };
           Console.WriteLine(FD("Kiwi"));
           Console.ReadKey();
        }       
    }

以上就是語法演進過程,希望這篇修練文能對跟我以前一樣看不懂Lambda的同好有所幫助。