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

C#에서 SortedDictionary를 반복하는 열거자를 가져옵니다.


SortedDictionary를 반복하는 열거자를 얻으려면 코드는 다음과 같습니다 -

시스템 사용, System.Collections 사용, System.Collections.Generic 사용, public class Demo { public static void Main(){ SortedDictionarysortedDict =new SortedDictionary(); sortedDict.Add(100, "모바일"); sortedDict.Add(200, "노트북"); sortedDict.Add(300, "데스크톱"); sortedDict.Add(400, "스피커"); sortedDict.Add(500, "헤드폰"); sortedDict.Add(600, "이어폰"); Console.WriteLine("SortedDictionary 키-값 쌍..."); IDictionaryEnumerator 데모Enum =sortedDict.GetEnumerator(); while (demoEnum.MoveNext()) Console.WriteLine("키 =" + demoEnum.Key + ", 값 =" + demoEnum.Value); }}

출력

이것은 다음과 같은 출력을 생성합니다 -

SortedDictionary 키-값 쌍...Key =100, Value =MobileKey =200, Value =LaptopKey =300, Value =DesktopKey =400, Value =SpeakersKey =500, Value =HeadphoneKey =600, Value =이어폰 

다른 예를 살펴보겠습니다 -

시스템 사용, System.Collections 사용, System.Collections.Generic 사용, public class Demo { public static void Main(){ SortedDictionary sortedDict =new SortedDictionary(); sortedDict.Add(100, 1); sortedDict.Add(200, 2); sortedDict.Add(300, 3); sortedDict.Add(400, 4); sortedDict.Add(500, 5); sortedDict.Add(600, 6); sortedDict.Add(700, 7); sortedDict.Add(800, 8); sortedDict.Add(900, 9); sortedDict.Add(1000, 10); Console.WriteLine("SortedDictionary 키-값 쌍..."); IDictionaryEnumerator 데모Enum =sortedDict.GetEnumerator(); while (demoEnum.MoveNext()) Console.WriteLine("키 =" + demoEnum.Key + ", 값 =" + demoEnum.Value); } }

출력

이것은 다음과 같은 출력을 생성합니다 -

SortedDictionary 키-값 쌍...Key =100, Value =1Key =200, Value =2Key =300, Value =3Key =400, Value =4Key =500, Value =5Key =600, Value =6Key =700 , 값 =7키 =800, 값 =8키 =900, 값 =9키 =1000, 값 =10