LogIn
I don't have account.

How to iterate SortedList in C#

DevSniper
119 Views

Using foreach loop

using System;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        var sortedList = new SortedList<string,string> { {"language","C#"}, {"topic","SortedList"} ,{"label","Beginner"} };
        Console.WriteLine(".......KeyValuePair........");
        foreach(KeyValuePair<string,string> kvp in sortedList) {
            Console.WriteLine( $"Key : {kvp.Key} Value : {kvp.Value}");
        }
        Console.WriteLine("............................");
        foreach(var item in sortedList) {
            Console.WriteLine( $"item {item} \n\t Key : {item.Key} Value : {item.Value}");
        }
        Console.WriteLine("....Iterate over keys.......");
        foreach(var key in sortedList.Keys) {
            Console.WriteLine( $"key :- {key}");
        }
        Console.WriteLine("....Iterate over Values.....");
        foreach(var value in sortedList.Values) {
            Console.WriteLine( $"Value :- {value}");
        }
    }
}
.......KeyValuePair........
Key : label Value : Beginner
Key : language Value : C#
Key : topic Value : SortedList
............................
item [label, Beginner] 
	 Key : label Value : Beginner
item [language, C#] 
	 Key : language Value : C#
item [topic, SortedList] 
	 Key : topic Value : SortedList
....Iterate over keys.......
key :- label
key :- language
key :- topic
....Iterate over Values.....
Value :- Beginner
Value :- C#
Value :- SortedList

Using Linq (ElementAt method ) and Count Property

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        var sortedList = new SortedList<string,string> {
            {"language","C#"},
            {"topic","SortedList"},
            {"label","Beginner"}
        };
        Console.WriteLine("..........................");
        for(int i=0; i<sortedList.Count; i++) {
            var  item = sortedList.ElementAt(i);
            Console.WriteLine("Key : "+item.Key+" Value : "+item.Value);
        }
        Console.WriteLine(".......KeyValuePair........");
        for(int i=0; i<sortedList.Count; i++) {
            KeyValuePair<string , string> item  = sortedList.ElementAt(i);
            Console.WriteLine("Key : "+item.Key+" Value : "+item.Value);
        }
    }
}
..........................
Key : label Value : Beginner
Key : language Value : C#
Key : topic Value : SortedList
.......KeyValuePair........
Key : label Value : Beginner
Key : language Value : C#
Key : topic Value : SortedList

Using Linq ParallelEnumerable.ForAll Method

This method invokes in parallel the specified action for each element in the source and It is an efficient way to iterate a large data set. due to parallel execution, order of processing elements will be vary every time.

Signature of ForAll :-
public static void ForAll<TSource> (this System.Linq.ParallelQuery<TSource> source, Action<TSource> action);
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        var sortedList = new SortedList<string,string> {
            {"language","C#"},
            {"topic","SortedList"},
            {"label","Beginner"}
        };
        Console.WriteLine("..........................");
        sortedList.AsParallel().ForAll(d => Console.WriteLine("item "+d+"\n\t Key : "+d.Key+" Value : "+d.Value));
    }
}
..........................
item [topic, SortedList]
	 Key : topic Value : SortedList
item [language, C#]
	 Key : language Value : C#
item [label, Beginner]
	 Key : label Value : Beginner

String.Join Method

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        var sortedList = new SortedList<string,string> {
            {"language","C#"},
            {"topic","SortedList"},
            {"label","Beginner"}
        };
        Console.WriteLine(".....String.Join on elements.....");
        Console.WriteLine(String.Join(", ", sortedList));
        Console.WriteLine(".....String.Join on Keys.....");
        Console.WriteLine(String.Join(",", sortedList.Keys));
        Console.WriteLine(".....String.Join on Values.....");
        Console.WriteLine(String.Join(",", sortedList.Values));
    }
}
.....String.Join on elements.....
[label, Beginner], [language, C#], [topic, SortedList]
.....String.Join on Keys.....
label,language,topic
.....String.Join on Values.....
Beginner,C#,SortedList