C#/수업내용

03/23 List<T>.Find(Predicate<T>) 메서드 연습

박준희 2021. 3. 23. 12:46
728x90

Program.cs

using System;

namespace Study07
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Program");
            new App();
        }
    }
}

App.cs

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

namespace Study07
{
    public class Book
    {
        public string ID { get; set; }
        public string Title { get; set; }
        
    }
    public class App
    {        
        //생성자 
        public App()
        {
            Console.WriteLine("App");
            //컬렉션 인스턴스화
            List<Book> books = new List<Book>();

            //books.Find(delegate (Book book)
            //{
            //    return book.ID == "bk109";
            //});
            books.Add(new Book() { ID = "bk109", Title = "어린왕자" });

            Book foundBook = books.Find((book) => book.ID == "bk109");
            if (foundBook != null)
            {
                Console.WriteLine("{0}, {1}", foundBook.ID, foundBook.Title);
            }            
        }

        
    }
}
728x90