C# List中FindAll用法的一些簡單示例
如下所示:
using System;
using System.Collections.Generic;
public partial class List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateList();
}
private void CreateList()
{
List<string> list = new List<string>();
list.Add("Compsognathus");
list.Add("Amargasaurus");
list.Add("Oviraptor");
list.Add("Velociraptor");
list.Add("Deinonychus");
list.Add("Dilophosaurus");
list.Add("Gallimimus");
list.Add("Triceratops");
//循環(huán)輸出每一項
Response.Write("分別輸出每一項:");
foreach (string str in list)
{
Response.Write(str + ";");
}
//查找字符串中包含saurus的字符,利用了匿名方法(第一種方式)
List<string> listFind = list.FindAll(delegate(string s){
return s.Contains("saurus");
});
Response.Write("查找到的字符串為:");
foreach (string str in listFind)
{
Response.Write(str+" ;");
}
//第二種方式,這兩種方式實際上是等價的
Response.Write("</br>FindAll(EndWithSaurus):");
List<string> subList = list.FindAll(EndWithSaurus);//傳入了一個方法名
foreach (string str in subList)
{
Response.Write(str+" ;");
}
}
private bool EndWithSaurus(string s)
{
if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == "saurus"))
return true;
else
return false;
}
}
相關(guān)文章
C#?使用PrintDocument類打印標(biāo)簽的方法
本文介紹打印機初步配置,以及實現(xiàn)方法,標(biāo)簽主要展示資產(chǎn)基本信息以及二維碼,對C#?使用PrintDocument類打印標(biāo)簽的詳細(xì)過程感興趣的朋友一起看看吧2022-04-04Graphics.DrawImage繪制的圖像變大的原因分析及解決
這篇文章主要介紹了Graphics.DrawImage繪制的圖像變大的原因分析及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11C#事件標(biāo)準(zhǔn)命名規(guī)則及說明(包括用作事件類型的委托命名)
這篇文章主要介紹了C#事件標(biāo)準(zhǔn)命名規(guī)則及說明(包括用作事件類型的委托命名),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02