473,320 Members | 1,535 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,320 software developers and data experts.

List<>.Exists()???

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<longnumlist = 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.
Dec 4 '07 #1
6 9291
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
Dec 4 '07 #2
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
"Marc Gravell" wrote:
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
Dec 4 '07 #3

"Marc Gravell" <ma**********@gmail.comwrote in message
news:O3**************@TK2MSFTNGP05.phx.gbl...
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

Dec 4 '07 #4
Well, MSDN2 has a wiki-esque section... you could submit such a
remark?

Marc
Dec 4 '07 #5
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

"A" <a@homewrote in message news:uM**************@TK2MSFTNGP06.phx.gbl...
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<longnumlist = 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.

Dec 4 '07 #6
On Tue, 04 Dec 2007 07:39:12 -0800, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
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
Dec 4 '07 #7

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

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
2
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
3
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface - TestClass : IPerson I put this class in a...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
0
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.