Reputation: 11471
I have a set of information that I need to store in some kind of collection, the problem is that i am not allowed to create a separate class for it because they say "i could mess up the structure and design of other things" so I have an integer and a string and what I want to do is to be able to store them like this
index or similar int string
index[0] | 1 | "Bear, Person, Bird"|
index[1] | 2 | "Pear, Apples, Lime"|
The question is, is there a collection of some type for which I can store data like this without the need of a class so that i can reference it like this
myWeirdList.add(2,"Test, test, Test, test");
or
myWeirdArray.add(3,"roco,paco");
I hope the questions is clear if not I will keep an eye to better clarify..
Upvotes: 3
Views: 5570
Reputation: 3704
// Create a new dictionary
Dictionary<int,string> myWeirdList = new Dictionary<int, string>();
// Add items to it
myWeirdList.Add(2, "Test, test, Test, test");
// Retrieve text using key
var text_using_key = myWeirdList[2];
// Retrieve text using index
var text_using_index = myWeirdList.ElementAt(0).Value;
Upvotes: 2
Reputation: 52270
as Tim said for .net 4.0 there are Tuples:
var myTupleList = new List<Tuple<int, string>();
myTupleList.Add(Tuple.Create(2, "Test, test, Test, test");
if not you can allways use just object and box:
var myObjList = new ArrayList();
myObjList.Add(2);
myObjList.Add("Test, test, Test, test");
And if all other fails just make a private struct yourself - I just don't know how you could mess up some other design with this.
Upvotes: 6
Reputation: 16848
You could use either object or dynamic if you're using .Net 4.0.
Alternatively you might consider using an array of Dictionary items where an array entry is of type <int, string>
.
Upvotes: 2