Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# list Dictionary arrays
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 2:23am
Edited 6-Jun-13 2:33am
Ron Beyer58.7K
v2
Comments
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.
 
—SA
Rate this: bad
good
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>());
 
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]
  Permalink  
v4
Comments
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
   
+5
Rate this: bad
good
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[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();
   }
  Permalink  
Comments
Maciej Los at 6-Jun-13 8:07am
   
Good alternative ;)
+5
s_mon at 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)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 CPallini 155
3 PIEBALDconsult 150
4 BillWoodruff 148
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


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