TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# ToDictionary Method

Use the ToDictionary extension from System.Linq to set keys and values with a lambda.
ToDictionary. This extension method converts a collection into a Dictionary. It works on IEnumerable collections such as arrays and Lists.ToArrayToList
Some uses. We can use ToDictionary to optimize performance—while retaining short and clear code. This method simplifies the demands of the code.DictionaryIEnumerableArrayList
First example. Building up dictionaries can require a significant amount of code. When we use ToDictionary, we can use less code to create a Dictionary.

Important: The 2 arguments to ToDictionary are lambdas: the first sets each key, and the second sets each value.

Lambda: Look at the "v => v" style lines. Lambda can be expressed as "goes to," meaning each item in the array goes to itself.

Lambda
C# program that uses ToDictionary using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // Example integer array. int[] values = new int[] { 1, 3, 5, 7 }; // First argument is the key, second the value. Dictionary<int, bool> dictionary = values.ToDictionary(v => v, v => true); // Display all keys and values. foreach (KeyValuePair<int, bool> pair in dictionary) { Console.WriteLine(pair); } } } Output [1, True] [3, True] [5, True] [7, True]
String example. Dictionaries are most useful for strings and string lookups. This allows us to use a number (hash code) in place of a string, greatly speeding things up.

Also: Here we use the var keyword to simplify the syntax. This reduces repetitive syntax.

Var
C# program that uses ToDictionary and List using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // Example with strings and List. List<string> list = new List<string>() { "cat", "dog", "animal" }; var animals = list.ToDictionary(x => x, x => true); if (animals.ContainsKey("dog")) { // This is in the Dictionary. Console.WriteLine("dog exists"); } } } Output dog exists
LINQ. This method allows us to use fewer lines of code to insert elements into a Dictionary. This reduces lines of code and typos. It is elegant and fits well with other LINQ code.

Warning: LINQ methods are significantly slower, but will scale equally well. Some programmers may not understand lambda syntax.

LINQ

And: Sometimes, using ToDictionary may prevent you from combining the loop with another operation, also leading to inefficient code.

A summary. We used ToDictionary to transform a collection (such as an array or List) into a Dictionary collection. This provides constant-time lookups.Convert
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf