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. 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
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
"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
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
"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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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>....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
|
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...
| |