Click here to Skip to main content
11,640,521 members (59,967 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# list Dictionary array
Hi, i am new for programming in C# and i need a help to create a Dictionary list to store some string data. see the following example

row1-> "cat","dog","rat"
row2-> "bus"
row3-> "sunday","monday","tuesday","friday"

like that with different number of column.
please tell me how to create an object of dictionary and store data as above and how to read it also . thanks.
Posted 6-Jun-13 1:23am
Edited 6-Jun-13 1:33am
Ron Beyer60.5K
Sergey Alexandrovich Kryukov at 6-Jun-13 7:43am
An example is not a definition of a problem. When you formally define a problem, maybe if would make a decision closer.

Do you want to store all this data in memory, or somewhere else, for example in the database, in a file? What operations are required? What operations need to be optimized for performance?

And start with the goal of it all.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Do you mean something like that?!

var myList = new Dictionary<String, List<String>>();
myList.Add("row1", new List<String>());
myList.Add("row2", new List<String>());
myList.Add("row3", new List<String>());
Console.WriteLine("Number of rows -> " + myList.Count);
foreach(var row in myList) {
    Console.WriteLine("Number of columns in " + row.Key + " -> " + row.Value.Count);

[edit]fleshing out solution[/edit]
Tharindu Prabash at 6-Jun-13 7:47am
yes s_mon, thanks. this is what i finding,. and can you tell me how to read the number of rows and cols in a dictionary ? and how to read a element also
Maciej Los at 6-Jun-13 8:01am
Use List Item[^] property.
s_mon at 6-Jun-13 8:04am
Jep :)
Maciej Los at 6-Jun-13 8:06am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You don't need a Dictionary: just use a List < List < string > >. e.g.
   List<List<string>> m = new List<List<string>>();
   m.Add(new List<string>());
   m.Add(new List<string>());
   m.Add(new List<string>());
   m[0].AddRange( new string[] {"cat", "dog", "rat"});
   m[2].AddRange(new string[] { "sunday", "monday", "tuesday", "friday" });
   foreach (var row in m)
     foreach (var s in row)
       Console.Write("{0} ", s);
Maciej Los at 6-Jun-13 8:07am
Good alternative ;)
s_mon at 6-Jun-13 8:08am
Also usable :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mika Wendelius 370
1 OriginalGriff 205
2 Afzaal Ahmad Zeeshan 130
3 CPallini 125
4 DamithSL 105
0 Mika Wendelius 370
1 OriginalGriff 205
2 CPallini 125
3 DamithSL 105
4 Afzaal Ahmad Zeeshan 70

Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 6 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100