How to print KeyValuePair list by index in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main() {
        var list = new List<KeyValuePair<string, int>>();
        
        list.Add(new KeyValuePair<string, int>("c", 6));
        list.Add(new KeyValuePair<string, int>("java", 3));
        list.Add(new KeyValuePair<string, int>("c#", 9));
        list.Add(new KeyValuePair<string, int>("rust", 2));
        list.Add(new KeyValuePair<string, int>("c++", 7));

        int list_size = list.Count; 
        
        for (int i = 0; i < list_size; i++) {
            Console.WriteLine(list.ElementAt(i).Key + " : " + list.ElementAt(i).Value);
        }
    }
}




/*
run:

c : 6
java : 3
c# : 9
rust : 2
c++ : 7

*/

 



answered Apr 29, 2023 by avibootz

Related questions

1 answer 220 views
1 answer 296 views
1 answer 185 views
1 answer 177 views
177 views asked Apr 29, 2023 by avibootz
1 answer 225 views
2 answers 365 views
365 views asked Aug 24, 2018 by avibootz
2 answers 356 views
...