2번째 간단한 예제 나갑니다.
아래의 예제는 "List 제네릭을 이용한 간단한 예제 - 1" 강좌에서 언급한 소스 내용이니 여기서
간단히 넘어가겠습니다.
static void Main(string[] args){List<Person> pList = new List<Person>();pList.Add(new Person(1, "John", "", "Shields", 29, 'M'));pList.Add(new Person(2, "Mary", "Matthew", "Jacobs", 35, 'F'));pList.Add(new Person(3, "Amber", "Carl", "Agar", 25, 'M'));pList.Add(new Person(4, "Kathy", "", "Berry", 21, 'F'));pList.Add(new Person(5, "Lena", "Ashco", "Bilton", 33, 'F'));pList.Add(new Person(6, "Susanne", "", "Buck", 45, 'F'));pList.Add(new Person(7, "Jim", "", "Brown", 38, 'M'));pList.Add(new Person(8, "Jane", "G", "Hooks", 32, 'F'));pList.Add(new Person(9, "Robert", "", "", 31, 'M'));pList.Add(new Person(10, "Cindy", "Preston", "Fox", 25, 'F'));pList.Add(new Person(11, "Gina", "", "Austin", 27, 'F'));pList.Add(new Person(12, "Joel", "David", "Benson", 33, 'M'));pList.Add(new Person(13, "George", "R", "Douglas", 55, 'M'));pList.Add(new Person(14, "Richard", "", "Banks", 22, 'M'));pList.Add(new Person(15, "Mary", "C", "Shaw", 39, 'F'));}
위의 List 제네릭 값을 가지고 두개의 조건문을 구성하여 결과값을 얻도록 할까 합니다.
List<Person> filterMultiple = pList.FindAll(delegate(Person p) { return p.Age > 35 && p.Sex =='F'; });
PrintOnConsole(filterMultiple, "3. --- Filtering List<T> on multiple conditions (Age > 35 and Sex is Female) ---");
조건문은 나이가 35보다 크면서 여자인 명단을 추출하는 것입니다. 둘 다 조건에 만족하는 값을 얻기 위해서 && 조건문 키워드를사용했습니다. 둘 중 하나라도 만족하는 결과값을 얻고자 한다면 || 조건문 키워드를 사용하시면 되겠습니다.
PrintOnConsole(sortFName, "4. --- Sort List<T> (Sort on FirstName) ---"); FirstName 인 첫번째 이름에 대해서 정렬을 하는 구문입니다. 이는 asc 와 같은 효과를 얻을 수 있습니다. 해당 Sort 메서드는 QuickSort 알고리즘을 사용하는 Array.Sort 를 사용합니다. 구현은 두개의 동일한 Person 제네릭을 이용하여 비교를 함으로써 정렬을 합니다. 반대로 desc 와 같은 효과를 얻고자 한다면 어떻게 할까요? 이는 p2 가 p1 을 비교하도록 하며 sort 메소드를 구현하면 됩니다. PrintOnConsole(sortLNameDesc, "5. --- Sort List<T> descending (Sort on LastName descending) ---"); 다음에는 추가와 삭제 그리고 읽기 전용으로 변경하는 부분을 이야기 할까 합니다. 감사합니다. http://cafe.daum.net/aspdotnet/JTk/365 posted by 심재운 (shimpark@gmail.com)
Interface(인터페이스)의 속성(Property)을 클래스에서 다중 상속하여 사용하기 (1) | 2009.02.14 |
---|---|
ENUM 키워드의 Description 를 이용하여 값을 가져오기 (0) | 2009.02.14 |
List Generic (제네릭) 을 이용한 간단한 예제 - 2 (0) | 2009.02.13 |
List 제네릭을 이용한 간단한 예제 - 1 (0) | 2009.02.13 |
C# 의 상속(Inheritance) 구현 Overriding 설명(4) (0) | 2009.02.12 |
C# 의 상속(Inheritance) 구현 base 키워드설명(3) (0) | 2009.02.12 |
본문과 관련 있는 내용으로 댓글을 남겨주시면 감사하겠습니다.