Click here to Skip to main content
11,806,590 members (57,270 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Dictionary
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 Beyer61.1K
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 ppolymorphe 245
1 OriginalGriff 150
2 George Jonsson 145
3 CPallini 100
4 Abhinav S 80
0 OriginalGriff 3,380
1 Maciej Los 1,990
2 CPallini 1,915
3 KrunalRohit 1,907
4 ppolymorphe 1,317

Advertise | Privacy | Mobile
Web03 | 2.8.151002.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