C# List의 FindAll 사용 예제

1916 단어
다음과 같습니다.
 
  
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 list = new List();
        list.Add("Compsognathus");
        list.Add("Amargasaurus");
        list.Add("Oviraptor");
        list.Add("Velociraptor");
        list.Add("Deinonychus");
        list.Add("Dilophosaurus");
        list.Add("Gallimimus");
        list.Add("Triceratops");

        //
        Response.Write(" :");
        foreach (string str in list)
        {
            Response.Write(str + ";");
        }

        // saurus , ( )
        List listFind = list.FindAll(delegate(string s){
           return s.Contains("saurus");
        });

        Response.Write(" :");
        foreach (string str in listFind)
        {
            Response.Write(str+" ;");
        }

        // ,
        Response.Write("FindAll(EndWithSaurus):");
        List 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;
    }
}

좋은 웹페이지 즐겨찾기