Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
ToDictionary method in C#
The ToDictionary method is an extension method in C# and converts a collection into Dictionary.
Firstly, create a string array −
string[] str = new string[] {"Car", "Bus", "Bicycle"};
Now, use the Dictionary method to convert a collection to Dictionary −
str.ToDictionary(item => item, item => true);
Here is the complete code −
Example
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);
}
}
}
Output
Car, True Bus, True Bicycle, True
Advertisements
