Re: List<>.Exists()???
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Tue, 4 Dec 2007 09:39:12 -0600
"Marc Gravell" <marc.gravell@xxxxxxxxx> wrote in message
news:O3nwLkkNIHA.3940@xxxxxxxxxxxxxxxxxxxxxxx
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".
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
.
- Follow-Ups:
- Re: List<>.Exists()???
- From: Peter Duniho
- Re: List<>.Exists()???
- From: Marc Gravell
- Re: List<>.Exists()???
- References:
- List<>.Exists()???
- From: A
- Re: List<>.Exists()???
- From: Marc Gravell
- List<>.Exists()???
- Prev by Date: Re: Does Application.UseWaitCursor affect all threads in an application
- Next by Date: Re: Using SQlite in c#
- Previous by thread: Re: List<>.Exists()???
- Next by thread: Re: List<>.Exists()???
- Index(es):
Relevant Pages
|