"c# iterate sortedDictionary" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "c# iterate sortedDictionary" answered properly. Developers are finding an appropriate answer about c# iterate sortedDictionary related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like c# iterate sortedDictionary. Enter your desired code related query in the search bar and get every piece of information about C# code related question on c# iterate sortedDictionary. 

c# iterate sortedDictionary

By AnwyAnwy on Feb 24, 2021
using System;
using System.Collections.Generic;

class SortedDictionaryEnumerationDemo
{
    static void Main()
    {
      	//Creates new SortedDictionary
        var dict = new SortedDictionary<int, string>();
        dict.Add(4, "Four");
        dict.Add(5, "Five");
        dict.Add(1, "One");
        dict.Add(3, "Three");
        dict.Add(2, "Two");

      	//Enumerating Items
        Console.WriteLine("== Enumerating Items ==");
        foreach (var item in dict)
        {
            Console.WriteLine("{0} => {1}", item.Key, item.Value);
        }

      	//Enumerating Keys
        Console.WriteLine("\n== Enumerating Keys ==");
        foreach (int key in dict.Keys)
        {
            Console.WriteLine("{0} => {1}", key, dict[key]);
        }

      	//Enumerating Values
        Console.WriteLine("\n== Enumerating Values ==");
        foreach (string value in dict.Values)
        {
            Console.WriteLine("{0} => {1}", value, GetKeyFromValue(dict, value));
        }
    }
     
    //Help method for Enumerating Values
    static int GetKeyFromValue(SortedDictionary<int, string> dict, string value)
    {
        // Use LINQ to do a reverse dictionary lookup.
        try
        {
            return
                (from item in dict
                 where item.Value.Equals(value)
                 select item.Key).First();
        }
        catch (InvalidOperationException e)
        {
            return -1;
        }
    }

}

//Console: 
/*
== Enumerating Items ==
1 => One
2 => Two
3 => Three
4 => Four
5 => Five

== Enumerating Keys ==
1 => One
2 => Two
3 => Three
4 => Four
5 => Five

== Enumerating Values ==
One => 1
Two => 2
Three => 3
Four => 4
Five => 5
*/

Source: stackoverflow.com

Add Comment

0

All those coders who are working on the C# based application and are stuck on c# iterate sortedDictionary can get a collection of related answers to their query. Programmers need to enter their query on c# iterate sortedDictionary related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about c# iterate sortedDictionary for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of c# iterate sortedDictionary while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "c# iterate sortedDictionary". Visit this developer's friendly online web community, CodeProZone, and get your queries like c# iterate sortedDictionary resolved professionally and stay updated to the latest C# updates. 

C# answers related to "c# iterate sortedDictionary"

View All C# queries

C# queries related to "c# iterate sortedDictionary"

Browse Other Code Languages

CodeProZone