3

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..

5
  • 2
    If you're using C# 4.0, you can check out the Tuple Class. Commented Sep 7, 2011 at 5:22
  • 3
    Have you had a look at a Dictionary? msdn.microsoft.com/en-us/library/xfhwa508.aspx Commented Sep 7, 2011 at 5:24
  • Wow guys really interesting concepts... Commented Sep 7, 2011 at 5:29
  • Agree with Tim - array of Tuple<int, string> should do the trick! Commented Sep 7, 2011 at 5:42
  • 1
    I think "they" introduce even more trouble by not using a custom class Commented Sep 7, 2011 at 6:19

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

One question, so if I need to pass this to a property (the items), what should the property type be? List or Tuple?.. if i understand correctly..
if you want the Tuple-List than your type would have to be List<Tuple<int, string>>
2

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>.

Comments

2
// 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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.