C# List中FindAll用法的一些簡(jiǎn)單示例
如下所示:
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)輸出每一項(xiàng)
Response.Write("分別輸出每一項(xiàng):");
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+" ;");
}
//第二種方式,這兩種方式實(shí)際上是等價(jià)的
Response.Write("</br>FindAll(EndWithSaurus):");
List<string> subList = list.FindAll(EndWithSaurus);//傳入了一個(gè)方法名
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#訪問(wèn)SqlServer設(shè)置鏈接超時(shí)的方法
這篇文章主要介紹了C#訪問(wèn)SqlServer設(shè)置鏈接超時(shí)的方法,涉及CommandTimeout屬性的相關(guān)設(shè)置技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-06-06C#?使用PrintDocument類(lèi)打印標(biāo)簽的方法
本文介紹打印機(jī)初步配置,以及實(shí)現(xiàn)方法,標(biāo)簽主要展示資產(chǎn)基本信息以及二維碼,對(duì)C#?使用PrintDocument類(lèi)打印標(biāo)簽的詳細(xì)過(guò)程感興趣的朋友一起看看吧2022-04-04Graphics.DrawImage繪制的圖像變大的原因分析及解決
這篇文章主要介紹了Graphics.DrawImage繪制的圖像變大的原因分析及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11C#事件標(biāo)準(zhǔn)命名規(guī)則及說(shuō)明(包括用作事件類(lèi)型的委托命名)
這篇文章主要介紹了C#事件標(biāo)準(zhǔn)命名規(guī)則及說(shuō)明(包括用作事件類(lèi)型的委托命名),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
這篇文章主要介紹了C#實(shí)現(xiàn)文件壓縮與解壓的方法,結(jié)合具體實(shí)例形式分析了C#針對(duì)文件進(jìn)行zip格式壓縮與解壓縮的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06