C# 中的 ToDictionary 方法
ToDictionary 方法是 C# 中的一个扩展方法,可以将一个集合转换成字典。
首先,创建一个字符串数组 −
string[] str = new string[] {"Car", "Bus", "Bicycle"};现在,使用 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
JavaScript
PHP