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# Map Example

Use a Dictionary as a map of keys to values. A map collection is a hash table.
Map. How can we map keys to values in a C# program? The C# language has no built-in map type. But it offers a powerful Dictionary type, which we use to map things.
With Dictionary, we must specify the type of the key (like "string") and the type of the value (like "int"). With Add we map a key to a value. With TryGetValue we safely check this mapping.
Map example. Here we map strings to other strings. Each string can only have a mapping to one other string (keys must be unique).

Cat, dog: We use the Add method to map the "cat" and "dog" strings to some words that indicate color.

Foreach: We loop over all keys and values in the map. We access each KeyValuePair in the map in the foreach loop.

KeyValuePair

TryGetValue: This is the best method to access a value in the map from a key. It returns false if the key does not exist.

TryGetValue
C# program that uses Dictionary as map using System; using System.Collections.Generic; class Program { static void Main() { // Use Dictionary as a map. var map = new Dictionary<string, string>(); // ... Add some keys and values. map.Add("cat", "orange"); map.Add("dog", "brown"); // ... Loop over the map. foreach (var pair in map) { string key = pair.Key; string value = pair.Value; Console.WriteLine(key + "/" + value); } // ... Get value at a known key. string result = map["cat"]; Console.WriteLine(result); // ... Use TryGetValue to safely look up a value in the map. string mapValue; if (map.TryGetValue("dog", out mapValue)) { Console.WriteLine(mapValue); } } } Output cat/orange dog/brown orange brown
Some notes. Dictionary is an important part of C# programming. It is a map collection. Usually the best way to access it is with TryGetValue—this is safe and does not throw exceptions.
For a map function, we want a method that applies another method to each element in a collection. The Select method, part of LINQ, is ideal for mapping elements.Select
In this language, no map built-in exists. But we can access a Dictionary, or use the Select method from LINQ for all our mapping needs.Dictionary
© 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