How to use name value collection (NameValueCollection ) in C#

2 Answers

0 votes
using System;
using System.Collections.Specialized;
   
class Program
{
    static void Main() {
        NameValueCollection nvc = new NameValueCollection();
          
        nvc.Add("c#", "2");
        nvc.Add("c", "1");
        nvc.Add("php", "5");
        nvc.Add("java", "4");
        nvc.Add("python", "3");
         
        foreach (string key in nvc) {
            Console.WriteLine(nvc[key]);
        }
    }
}
  
   
   
/*
run:
   
2
1
5
4
3
   
*/

 



answered Sep 12, 2019 by avibootz
edited Sep 12, 2019 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Specialized;
   
class Program
{
    static void Main() {
        NameValueCollection nvc = new NameValueCollection();
          
        nvc.Add("c#", "2");
        nvc.Add("c", "1");
        nvc.Add("php", "5");
        nvc.Add("java", "4");
        nvc.Add("python", "3");
         
        var items = nvc.AllKeys.SelectMany(nvc.GetValues, (k, v) => new {key = k, value = v});
        
        foreach (var item in items)
            Console.WriteLine(item.key + " - " + item.value);
    }
}
  
   
   
/*
run:
   
c# - 2
c - 1
php - 5
java - 4
python - 3
   
*/

 



answered Sep 12, 2019 by avibootz

Related questions

1 answer 191 views
1 answer 83 views
83 views asked Mar 26, 2024 by avibootz
1 answer 101 views
1 answer 116 views
1 answer 131 views
131 views asked Apr 21, 2020 by avibootz
...