List<>.Exists()???

  • Thread starter Thread starter A
  • Start date Start date
A

A

Hi,
I don't get this method... Could someone please explain it?

I have a number generator where I want to add the numbers to a List<long>.
Before I add each number, I want to make sure that the number does not
already exist. Sample code:

List<long> numlist = new List<long>();
long newNumber = generator.New(); // generates the new number

if (!numlist.Exists(newNumber)) // obviously does not work, that's my
problem.
numlist.Add(newNumber);

Can anyone help me?

Thank you in advance.
 
What you are looking for is List<T>.Contains(...), i.e.

if(!numList.Contains(newNumber)) {...}

That should fix it; for completeness:
List<T>.Exists(...) checks whether any of the items in the list
satisfies a condition (specified as a predicate); for example, to test
whether any of the numbers are divisible by 3 (using the new C# 3
"lambda" syntax):

if (numList.Exists(i => i % 3 == 0)) {...}

The "predicate" is just a method that accepts the item to be tested
and returns true (match) or false:

static bool SomeMethod(int i) {
return i % 3 == 0;
}
....
if (numList.Exists(SomeMethod)) {...}

Does that make it any clearer?

Marc
 
Using C# 2.0 you can use anonymous methods as well

if (numList.Exists(delegate(int i) { return i % 3 == 0; })) { }

although for primitives and known references, Contains() is easier to use
and read.
 
Marc Gravell said:
What you are looking for is List<T>.Contains(...), i.e.


This answer has come up before...

I think it may have something to do with the fact that the help article for
Exists doesn't mention Contains anywhere, never mind as a "Remarks: To find
whether the list contains an element equal to a provided element, use the
Contains method".
 
Well, MSDN2 has a wiki-esque section... you could submit such a
remark?

Marc
 
Just to add to the previous responses it might be worth mentioning that
Contains is an O(n) operation. If you are generating a large list I'd
suggest you consider using a container class that can be searched more
quickly than this (Dictionary<> or Hashtable perhaps)

Cheers
Doug Forster
 
This answer has come up before...

I think it may have something to do with the fact that the help article
for
Exists doesn't mention Contains anywhere, never mind as a "Remarks: To
find
whether the list contains an element equal to a provided element, use the
Contains method".

Well, that and for some reason it seems that people frequently overlook
that for each class there's a doc page that provides a complete list of
all members of the class.

For me, that's almost always the first place I go (assuming I didn't find
what I was looking for directly in VS using the Intellisense feature) if
I'm trying to find out if there's some method or something that does what
I want.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top