Click here to Skip to main content
13,141,354 members (88,231 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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
Updated 6-Jun-13 1:33am
Ron Beyer65.3K
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 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 6-Jun-13 8:01am
Use List<t> Item[^] property.
s_mon 6-Jun-13 8:04am
Jep :)
Maciej Los 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 6-Jun-13 8:07am
Good alternative ;)
s_mon 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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 6 Jun 2013
Copyright © CodeProject, 1999-2017
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