Some simple examples of FindAll usage in C List

  • 2020-06-07 05:14:03
  • OfStack

As shown below:


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");
        // Cyclic output per 1 item 
        Response.Write(" Output each separately 1 Item: ");
        foreach (string str in list)
        {
            Response.Write(str + ";");
        }
        // Find string contains saurus The character of , Anonymous methods are used ( The first 1 Way) 
        List<string> listFind = list.FindAll(delegate(string s){
           return s.Contains("saurus");
        });
        Response.Write(" The found string is: ");
        foreach (string str in listFind)
        {
            Response.Write(str+" ;");
        }
        // The first 2 These two ways, these two ways are actually equivalent 
        Response.Write("</br>FindAll(EndWithSaurus):");
        List<string> subList = list.FindAll(EndWithSaurus);// Introduced to the 1 A method name 
        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;
    }
}


Related articles: