ToDictionary 메서드는 C#의 확장 메서드이며 컬렉션을 사전으로 변환합니다.
먼저 문자열 배열을 만듭니다 -
string[] str = new string[] {"Car", "Bus", "Bicycle"}; 이제 Dictionary 메서드를 사용하여 컬렉션을 Dictionary −
로 변환합니다.str.ToDictionary(item => item, item => true);
다음은 전체 코드입니다 -
예시
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
string[] str = new string[] {"Car", "Bus", "Bicycle"};
// key and value under ToDictionary
var d = str.ToDictionary(item => item, item => true);
foreach (var ele in d) {
Console.WriteLine("{0}, {1}", ele.Key, ele.Value);
}
}
} 출력
Car, True Bus, True Bicycle, True