473,416 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Why doesn't ICollection have a Contains method?

It's funny that I've only now run into this question, after a few years
of using C#, but I find it intriguiging all the same. All the more so,
that the generic version of ICollection in .Net Framework 2.0 has this
method.

It seems to me that the question whether a collection has some element
should be answerable by any collection, not just IList or IDictionary.
An obvious implementation must exist, since every ICollection inherits
IEnumerable.

I ran into the problem, where I wanted to use a collection as general
as possible (i.e. not limiting the user to go down the IList or
IDictionary path), but I need to tell whether it contains a certain
object. I can implement it myself, but wouldn't it be easier if
ICollection simply provided a method.

Does anyone have any comments? Are there any plans to add this method
in .Net Framework 2.0?

Thanks,

Andrew

Nov 17 '05 #1
3 5146
All its says is that you can travel the items (as IEnumerable),
and as a great concession it also gives you the Count,
and take a snapshot of its elements into an array.
It is therefore targeted to lists of items that can not be quickly found by
item.
Although there is an obvious implementation, that obvious implementation is
slow.
Requiring to find an item could be a significant constraint in
many cases, so as to avoid implementing it.

It also lets you get a synchronized collection.

That all seems me an enough forward step from IEnumerable.

The next step is IList.

best regards,
Alejandro Lapeyre

<an**************@gmail.com> escribió en el mensaje
news:11**********************@g47g2000cwa.googlegr oups.com...
It's funny that I've only now run into this question, after a few years
of using C#, but I find it intriguiging all the same. All the more so,
that the generic version of ICollection in .Net Framework 2.0 has this
method.

It seems to me that the question whether a collection has some element
should be answerable by any collection, not just IList or IDictionary.
An obvious implementation must exist, since every ICollection inherits
IEnumerable.

I ran into the problem, where I wanted to use a collection as general
as possible (i.e. not limiting the user to go down the IList or
IDictionary path), but I need to tell whether it contains a certain
object. I can implement it myself, but wouldn't it be easier if
ICollection simply provided a method.

Does anyone have any comments? Are there any plans to add this method
in .Net Framework 2.0?

Thanks,

Andrew

Nov 17 '05 #2
You may have misunderstood what I meant. I wasn't trying to say that
ICollection does not offer functionality beyond IEnumerable. Rather, I
believe that it should have a Contains method.

What you say about its implementation potentially being slow is true,
but it is also true for a generic IList interface, and yet it is
available there. I think that Contains is generic enough to be
available at the ICollection level instead of separately at the IList
and IDictionary levels, which imply a certain implementation. Say, I
have a function that takes a collection parameter. I don't care if it
is a list or dictionary, but I need to know if it contains a certain
item. In other words the lack of the Contains method forces me to user
either IList or IDictionary, where I could have used a more generic
ICollection, unless of course I implement the obvious (and slow) linear
search using IEnumerable myself.

Generally, I don't care how the Contains is implemented by the class
that implements ICollection. I trust that it's implemented in the best
way possible, and in a way much better than what I can come up with not
knowing anything specific about the given colleciton. Suppose that the
ICollection passed to my method is in fact a Hashtable. If I had
access to Contains via ICollection I could get the answer I need very
efficiently. However, if I have to use IEnumerable to implement my own
linear search, my version of Contains is going to be very slow.

Therefore, I believe Contains belongs in ICollection. I think that's
what motivated its inclusion in the IGenericCollection. I just wonder
why it doesn't show in ICollection even for .Net Framework 2.0.

Andrew

Nov 17 '05 #3
They can't add a new method to a interface since it would break all code
that uses this interface.
<an**************@gmail.com> schrieb im Newsbeitrag
news:11**********************@g47g2000cwa.googlegr oups.com...
It's funny that I've only now run into this question, after a few years
of using C#, but I find it intriguiging all the same. All the more so,
that the generic version of ICollection in .Net Framework 2.0 has this
method.

It seems to me that the question whether a collection has some element
should be answerable by any collection, not just IList or IDictionary.
An obvious implementation must exist, since every ICollection inherits
IEnumerable.

I ran into the problem, where I wanted to use a collection as general
as possible (i.e. not limiting the user to go down the IList or
IDictionary path), but I need to tell whether it contains a certain
object. I can implement it myself, but wouldn't it be easier if
ICollection simply provided a method.

Does anyone have any comments? Are there any plans to add this method
in .Net Framework 2.0?

Thanks,

Andrew

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
2
by: Nick | last post by:
Out of context and just asking but why does the following code work? Hashtable ht = new Hashtable(); ht.Add(1,1); .... .... ..... ICollection collection = ht.Values; Hashtable ht2 =...
7
by: MrNobody | last post by:
since ArrayList implements ICollection, is there a quick way to convert an ICollection into ArrayList (besides actually iterating through each element in the ICollection and explicitly adding them...
1
by: Sylvain | last post by:
Hi, I'm encountering a very simple issue with ArrayList constructor and AddRange() method overriding. I'm defining a class that extends ArrayList and contains one overriden method:...
1
by: Janus Knudsen | last post by:
Hello :O) Im in the process of learning asp.net, and i'm reading a nice book. But then suddenly i came across this: public ICollection DataLoad() { DataTable dtEmployee = new DataTable();
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
3
by: Dave Booker | last post by:
Am I missing something here? It looks like the generic Queue<T> implements Enumerable<T> and ICollection but not ICollection<T>. (I want to use it in an interface that wants an ICollection<T>.) ...
2
by: Allan Ebdrup | last post by:
I have a public sealed Class A that implements the interface I. Now I want to pass an ICollection<Aas a parameter to a method that takes a parameter of the type ICollection<Ibut I get the error: ...
3
by: Tony | last post by:
Hello! I'm reading in book and it says the following. "ICollection provides the ability to obtain the number of items in a collection and to copy items into a simple array type(inherits from...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.