Nice C # Collection

What a nice collection in C # for storing data below:

I have flags that bring an object identifier, varnumber, varname, and a header associated with each flag.

I need a collection that can be of any size, something like an ArrayList, maybe maybe:

list[i][subjectid] = x; list[i][varnumber] = x; list[i][varname] = x; list[i][title] = x; 

Any good ideas?

+7
source share
8 answers

A List<Mumble> , where Mumble is a small helper class that preserves properties.

 List<Mumble> list = new List<Mumble>(); ... var foo = new Mumble(subjectid); foo.varnumber = bar; ... list.Add(foo); ,.. list[i].varname = "something else"; 
+14
source
 public Class MyFields { public int SubjectID { get; set; } public int VarNumber { get; set; } public string VarName { get; set; } public string Title { get; set; } } var myList = new List<MyFields>(); 

To access a member:

 var myVarName = myList[i].VarName; 
+7
source

The general List<YourClass> list will be large - where YourClass has subjectid, varnumber, etc.

+1
source

You will probably want to use a two-dimensional array for this and allocate positions in the second dimension of the array for each of your values. For example, list[i][0] will be subjectid , list[i][1] will be varnumber , etc.

0
source

Determining which collection usually begins with what you want to do with it?

If your only criteria could be anysize, then I would consider List<>

0
source

Since this is a pair of Key, Value, I would recommend you use a common IDictionary-based collection.

 // Create a new dictionary of strings, with string keys, // and access it through the IDictionary generic interface. IDictionary<string, string> openWith = new Dictionary<string, string>(); // Add some elements to the dictionary. There are no // duplicate keys, but some of the values are duplicates. openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe"); 
0
source

As others have said, it seems that you better create a class to hold values ​​so that your list returns an object containing all the necessary data. Although two-dimensional arrays can be useful, this is not like one of these situations.

For more information on the best solution and why a two-dimensional array / list in this example is not a good idea, you can read: Create a list of objects instead of many lists of values

0
source

If there is a possibility that the order [i] not in a predictable order or possibly has spaces, but you need to use it as a key:

 public class Thing { int SubjectID { get; set; } int VarNumber { get; set; } string VarName { get; set; } string Title { get; set; } } Dictionary<int, Thing> things = new Dictionary<int, Thing>(); dict.Add(i, thing); 

Then, to find a Thing :

 var myThing = things[i]; 
0
source

All Articles