Taking beginning Data Structures in C# class, trying to make a searchable arraylist of a string and an int referenced together (name and number).I need to be able to search for the name, and retrieve both the name and the number it’s attached to. I was told I can use an arraylist of structs, but have never used structs. Any simple explanation how, or a better way?
Thank you in advance.
You should probably be using something like a hash for this as an
ArrayListof structs is a bit square peg round hole.You declare a struct just like a calss.
And you can make an array list of then as you would with any type
The other issue here is that
ArrayListisn’t generic, it will cast everything toObjectwhich means you will have to cast everything back to typeFooto work with it.You would be much better off, if you have to go the route of List instead of Hash using a generic list class like
Listand declaring its type;As for searching a list, I prefer to use the
LINQmethods, because they are quick to code and easy to read.But if you cannot do that then iteration always works