Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# iterate sortedDictionary

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("
== Enumerating Keys ==");
        foreach (int key in dict.Keys)
        {
            Console.WriteLine("{0} => {1}", key, dict[key]);
        }

      	//Enumerating Values
        Console.WriteLine("
== 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
*/
Comment

PREVIOUS NEXT
Code Example
Csharp :: mysqldump - date 
Csharp :: string in c# 
Csharp :: longest substring without repeating characters 
Csharp :: c# add list to list 
Csharp :: sieve of eratosthenes 
Csharp :: visitor pattern 
Csharp :: how to create url parameters for URi C# 
Csharp :: c sharp 
Csharp :: print pdf in c# 
Csharp :: vb.net center form in screen 
Csharp :: How can I display image from database in asp.net mvc. I created image table and image path as varchar 
Csharp :: preprocessors in c# 
Csharp :: jagged array to 2d array c# 
Csharp :: c# windows forms cancel event 
Csharp :: c# create class from parent class 
Csharp :: open linkedlabel c# 
Csharp :: Why Duplicate "..TargetFrameworkAttribute" c# assemblies created 
Csharp :: Get Component Trail rendere 
Csharp :: clickable table row asp.net core 
Csharp :: c# Remove String In C# 
Csharp :: eventsource web api c# 
Csharp :: how to pause a console.writeline in C# 
Csharp :: linear search algorithm c# 
Csharp :: unity find all scriptable objects of a type 
Csharp :: Unlit shader get the direction of camera UNity 
Csharp :: c# trimend substring 
Csharp :: 405 - HTTP verb used to access this page is not allowed 
Csharp :: use c#9 
Csharp :: c# switch expression pattern matching 
Csharp :: asp.net core update-database specify environment 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =