日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#中的匿名函數、lambda表達式解讀_C#教程

作者:Danny_hi ? 更新時間: 2023-03-20 編程語言

C# 匿名函數、lambda表達式、Linq查詢

一、匿名函數的使用

匿名函數是一個“內聯”語句或表達式,可在需要委托類型的任何地方使用。

可以使用匿名函數來初始化命名委托,或傳遞命名委托(而不是命名委托類型)作為方法參數。

下面的示例演示了從 C# 1.0 到 C# 3.0 委托創建過程的發展:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test0630
{
? ? delegate void TestDelegate(string s);
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? // Original delegate syntax required?
? ? ? ? ? ? // initialization with a named method.
? ? ? ? ? ? TestDelegate testDelA = new TestDelegate(M);

? ? ? ? ? ? // C# 2.0: A delegate can be initialized with
? ? ? ? ? ? // inline code, called an "anonymous method." This
? ? ? ? ? ? // method takes a string as an input parameter.
? ? ? ? ? ? TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };

? ? ? ? ? ? // C# 3.0. A delegate can be initialized with
? ? ? ? ? ? // a lambda expression. The lambda also takes a string
? ? ? ? ? ? // as an input parameter (x). The type of x is inferred by the compiler.
? ? ? ? ? ? TestDelegate testDelC = (x) => { Console.WriteLine(x); };

? ? ? ? ? ? // Invoke the delegates.
? ? ? ? ? ? testDelA("Hello,this is TestA");
? ? ? ? ? ? testDelB("Hello,this is TestB");
? ? ? ? ? ? testDelC("Hello,this is TestC");

? ? ? ? ? ? // Keep console window open in debug mode.
? ? ? ? ? ? Console.WriteLine("Press any key to exit.");
? ? ? ? ? ? Console.ReadKey();
? ? ? ? }


? ? ? ? static void M(string s)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(s);
? ? ? ? }
? ? }
}

二、lambda表達式

lambda表達式是一個匿名函數,是一種高效的類似于函數式編程的表達式,Lambda簡化了開發中需要編寫的代碼量,是LINQ的基礎。

lambda表達式格式:(參數列表)=>表達式或語句塊 ,舉例如下:

//無參
() => DoSomeThing() ;

//單參數
p => p.id > 0 ; //返回Bool

//多參數
( x , y ) => x * y ;

//帶類型輸入參數
( int x , int y ) => x * y;

下面介紹List集合中的Lambda表達式的運用:

(1) 查詢班級編號為1001的班級下面的所有學生實體并返回到list1001中存儲

var list1001=Studentlist.Where(t=>t.ClassCode==‘1001');

(2) 查詢班級編號為1001的班級下面的所有學生實體并返回到list1001中存儲,并按照學生的出生日期從小到大排列。

var list1001=Studentlist.Where(t=>t.ClassCode==‘1001').OrderBy(t=>t.BirthDay);

在此說一下,OrderBy是從小到大排序,需要從大到小排列則用OrderByDescending。

(3) 查詢班級編號為1001的班級下面的姓氏為【李】的同學的所有集合,并按照學生的出生日期從小到大排列。

var list1001=Studentlist.Where(t=>t.ClassCode==‘1001'&&t.StudentName.StartWith(“李”)).OrderBy(t=>t.BirthDay);

(4) 查詢出班級編號為1001的班級,并且存在至少一門考試科目成績低于60分的所有同學。

var result = studentList.Where(t => (t.ClassCode == "1001") && (scoreList.Exists(p => p.ScoreValue < 60 && p.StudentCode == t.StudentCode)));

(5) 其他較常用的Lambda表達式如下:

var a = studentList.FirstOrDefault(t => t.StudentCode == "10012");//FirstOrDefault返回第一個符合條件的數據,不存在的時候返回Null。
var b = studentList.Count(t => t.StudentName == "李世民");//返回符合條件的實體個數
var c = studentList.FindAll(t => t.StudentName.Contains("中"));//查找所有名字中含有【中】的實體集合
var d = studentList.GroupBy(t => t.ClassCode);//對studentList按照ClassCode分組
var f = studentList.Max(t => t.BirthDay);//返回最大的出生日期。
var e = scoreList.Sum(t => t.ScoreValue);//對所有成績求和
var g = scoreList.Average(t => t.ScoreValue);//對所有成績求平均分
var h = studentList.Select(t => t.StudentName).Distinct();//獲取所有的學生姓名,并去除重名

總結

原文鏈接:https://blog.csdn.net/qq_43024228/article/details/107035662

欄目分類
最近更新