- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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