리스트의 List<(Of <(T>)>)..::.Find 메서드 또는 FindAll 등은 대리자 메소드 입니다.

알고나면 정말정말 유용하게 사용가능!

 

Find 는 리스트에서 결과값을 찾음 빠져나가고 FindAll은 찾고나서도 끝까지 검색합니다.

 

 

예제

리스트형식이 string 형이면 string 형으로 반환합니다 그형에 맞게 반환 합니다.

그러므로 클래스 등등 다 찾을수 있습니다 ㅎ

   List<string> dinosaurs = new List<string>();

            dinosaurs.Add("Compsognathus");
            dinosaurs.Add("Amargasaurus");
            dinosaurs.Add("Oviraptor");
            dinosaurs.Add("Velociraptor");
            dinosaurs.Add("Deinonychus");
            dinosaurs.Add("Dilophosaurus");
            dinosaurs.Add("Gallimimus");
            dinosaurs.Add("Triceratops");

리스트를 생성합니다.

 Console.WriteLine("\nFind(EndsWithSaurus): {0}",
                dinosaurs.Find(EndsWithSaurus));

 

EndsWithSaurus 를 사용해서 검색을 합니다.

        private static bool  EndsWithSaurus(String s)
        {
            if (s == "Triceratops")
            {
               return true; //찾으면 true 리턴
            }
            return false;
          
        }

찾으면 true를 반환하고 있습니다.

아래와 같이 다양한 조건을 검색 할 수 있습니다.

        private static bool  EndsWithSaurus(String s)
        {
            if ((s.Length > 5) &&
                (s.Substring(s.Length - 6).ToLower() == "saurus"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

 

 

 

전체소스

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

namespace TESTConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> dinosaurs = new List<string>();

            dinosaurs.Add("Compsognathus");
            dinosaurs.Add("Amargasaurus");
            dinosaurs.Add("Oviraptor");
            dinosaurs.Add("Velociraptor");
            dinosaurs.Add("Deinonychus");
            dinosaurs.Add("Dilophosaurus");
            dinosaurs.Add("Gallimimus");
            dinosaurs.Add("Triceratops");
         


            Console.WriteLine("\nFind(EndsWithSaurus): {0}",
                dinosaurs.Find(EndsWithSaurus));



            Console.ReadLine();

        }

        // Search predicate returns true if a string ends in "saurus".
        private static bool  EndsWithSaurus(String s)
        {
            if ((s.Length > 5) &&
                (s.Substring(s.Length - 6).ToLower() == "saurus"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }

}

 

 

만약에 "saurus" 를 변경하고 싶다면?

변수를 넘겨야 하는데 넘길수가 없따; 무명대리자 이용 뭐 전역변수로 해도 되긴된다 ㅋ

 

방법

                dinosaurs.Find(

                 delegate(string s) { //무명대리자 이용

                     if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() ==searchopt))
                     {
                         return true;
                     }
                     else
                     {
                         return false;
                     }  
                 }


                ));   
  전체소스

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;

namespace TESTConsoleApplication
{
    public  class Program
    {
       static void Main(string[] args)   
        {   
            List<string> dinosaurs = new List<string>();   
  
            dinosaurs.Add("Compsognathus");   
            dinosaurs.Add("Amargasaurus");   
            dinosaurs.Add("Oviraptor");   
            dinosaurs.Add("Velociraptor");   
            dinosaurs.Add("Deinonychus");   
            dinosaurs.Add("Dilophosaurus");   
            dinosaurs.Add("Gallimimus");   
            dinosaurs.Add("Triceratops");


            string searchopt="saurus";
            Console.WriteLine("\nFind(EndsWithSaurus): {0}",
           
            

                dinosaurs.Find(

                 delegate(string s) { //무명대리자 이용

                     if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() ==searchopt))
                     {
                         return true;
                     }
                     else
                     {
                         return false;
                     }  
                 }


                ));   
  
  
  
            Console.ReadLine();   
  
        }   
  
        // Search predicate returns true if a string ends in "saurus".   
        private static bool  EndsWithSaurus(String s)   
        {   
            if ((s.Length > 5) &&   
                (s.Substring(s.Length - 6).ToLower() == "saurus"))   
            {   
                return true;   
            }   
            else  
            {   
                return false;   
            }   
        }   
  
    }   

      

    }

+ Recent posts