Picking an object from a list by reference and not by position

2
Hello, I would like to know how I can create a list of objects in C #, but instead of getting them by posição ([0], [i]) , I want to get by reference [LacoAzul] [CorVermelha] . Can you create a list like this?

Thanks in advance!

    
asked by anonymous 10.07.2014 / 14:52

1 answer

4

There are some Framework classes that work like this.

You can use a dictionary . In a dictionary there is no order. You access the elements by keys. The key can be of any type - if it is a type by reference, then you can pass the reference of an object to get the other object of the key - value .

Maybe a hash table (with class Hashtable ) also solves your problem. Like the dictionary it also works with key pairs - value , but it's a simpler structure. editing: Hashtable is now obsolete.

PS: if the purpose is just to know if the element is in the list, the generic list itself (class List ) has the Contains , which indicates whether or not an element is in the list from its reference.

    
10.07.2014 / 15:12