Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 페어 클래스

<시간/>

Pair 클래스는 C#을 사용하여 단일 목록에 값 쌍을 저장하는 KeyValuePair 클래스입니다.

KeyValuePair 선언 -

var myList =new Liststring, int>>(); 이제 몇 가지 요소를 추가합니다. myList.Add(new KeyValuePair("Laptop", 1));myList.Add(new KeyValuePair("데스크톱 시스템", 2));myList.Add(new KeyValuePair("태블릿", 3));myList.Add(new KeyValuePair("모바일 ", 4));myList.Add(new KeyValuePair("전자책 리더기", 5));myList.Add(new KeyValuePair("LED", 6)); 

아래와 같이 KeyValuePair를 표시하십시오. -

예시

시스템 사용, System.Collections.Generic 사용, 클래스 프로그램 { static void Main() { var myList =new List>(); // 요소 추가 myList.Add(new KeyValuePair ("Laptop", 1)); myList.Add(새로운 KeyValuePair <문자열, int>("데스크톱 시스템", 2)); myList.Add(새로운 KeyValuePair <문자열, int>("태블릿", 3)); myList.Add(새로운 KeyValuePair ("모바일", 4)); myList.Add(new KeyValuePair ("전자책 리더", 5)); myList.Add(새로운 KeyValuePair <문자열, 정수>("LED", 6)); foreach(myList의 var val) { Console.WriteLine(val); } }}

출력

[노트북, 1][데스크탑, 2][태블릿, 3][모바일, 4][전자책 리더, 5][LED, 6]