Implementing the IList Interface

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've developed a class in C# that implements the IList-interface because a Hashtable wasn't suitable for my task. However, I'm not sure that I developed my IList.Contains-method properly though.

The definition of IList.Contains is 'bool Contains(object value);' and the documentation states that the value is 'the object to locate in the IList'.

I was wondering what this 'value' is supposed to be. My collection contains a list of classes that each have an id-property. So, it would be easiest for me if the 'value' of 'Contains' could be this id. Can I do this or should the value be an actual instance of a class stored my collection?

I know that technically it can be anything, but are there any guidelines on what you should use? I don't want to use the id if there are other classes out there that would assume that the value is an actual instance instead of an id.
 
Hi Michael,
I've developed a class in C# that implements the IList-interface because a
Hashtable wasn't suitable for my task. However, I'm not sure that I
developed my IList.Contains-method properly though.
The definition of IList.Contains is 'bool Contains(object value);' and the
documentation states that the value is 'the object to locate in the IList'.
I was wondering what this 'value' is supposed to be. My collection
contains a list of classes that each have an id-property. So, it would be
easiest for me if the 'value' of 'Contains' could be this id. Can I do this
or should the value be an actual instance of a class stored my collection?
I know that technically it can be anything, but are there any guidelines
on what you should use? I don't want to use the id if there are other
classes out there that would assume that the value is an actual instance
instead of an id.

Well, it's a good question, and I'd say the answer depends quite a bit on
what use you expect to give to your class. Without knowing the context, I'd
probably stick to keeping it exactly as other IList implementations do (the
actual instance), and adding a new method to my class "ContainsId()" or
something that worked with the ID In the Value object (but, if that is the
case, it sounds like you'd be much more interested in an IDictionary
implementation, as opposed to just IList),.
 
Tomas, thank you for your post. Indeed, the thing I want to do is a lot like IDictionary. However, I didn't use that for the following reasons:
1. I wanted 'for each' to browse through my actual classes instead of through DictionaryEntry-objects.
2. I needed a Sort-method on my collection. Using this Sort-method I wanted to sort the items in my collection using any of their properties.
3. I wanted to be able to get items from the collections very quickly using their index AND using their Id.
4. I wanted a class that I can easily modify to use Generics when Whidbey comes out. I would like my class to work for now, but since we're not going to release within the next year I would like to be able to change it to Generics without having to completely rewrite dozens of these collections.

It would make things a lot easier for me if I could do myCollection.Contains(objMyClass.Id) because I don't always have a valid instance of MyClass, only an Id. In this case, I can't do myCollection.Contains(objMyClass), but I would like to do myCollection.Contains(intId).

Another reason why I don't want to do myCollection.Contains(objMyClass) is because I might have another instance of MyClass with the same property-values or the same id. In this case I would like myCollection.Contains(objMyClass) to return true even if it's a different instance of even if the Id is the only property that is the same. Because of this, myCollection.Contains(objMyClass) would have no reliable meaning for me. I'm a bit afraid to use myCollection.Contains(intId) though because I don't know for sure how other classes (like a DataGrid) would react if they bind to my collection.
 
Back
Top