Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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
Updated 6-Jun-13 1:33am
v2
Comments
Sergey Alexandrovich Kryukov 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.

—SA

Do you mean something like that?!

C#
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>());

myList["row1"].Add("cat");
myList["row1"].Add("dog");
myList["row1"].Add("rat");		

myList["row2"].Add("bus");		

myList["row3"].Add("sunday");
myList["row3"].Add("monday");
myList["row3"].Add("tuesday");
myList["row3"].Add("friday");

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]
 
Share this answer
 
v4
Comments
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.
StM0n 6-Jun-13 8:04am    
Jep :)
Maciej Los 6-Jun-13 8:06am    
+5
You don't need a Dictionary: just use a List < List < string > >. e.g.
C#
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[1].Add("bus");
m[2].AddRange(new string[] { "sunday", "monday", "tuesday", "friday" });
foreach (var row in m)
{
  foreach (var s in row)
    Console.Write("{0} ", s);
  Console.WriteLine();
}
 
Share this answer
 
Comments
Maciej Los 6-Jun-13 8:07am    
Good alternative ;)
+5
StM0n 6-Jun-13 8:08am    
Also usable :)
+5

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900