John Cantley <jo****@magenic.comwrote:
How do I use this, the example in msdn is basically worthless unless you
want to check something against a static string or something. I need to
check in my list if there is a duplicate entry, how do I do that?
I have a collection of supplier objects.
Supplier
Name
ID
Count
Score
I want to do
if (suppliers.Exist(CheckNewName))
do something
else
do some other thing
private static bool CheckNewName()
{
how do i check for dups here
}
I tried doing it like a compare but that doesn't work. Any ideas?
This is where anonymous methods comes in, as demonstrated by the
following code. I've put the anonymous method on a line on its own to
make it easier to see - it doesn't have to be.
using System;
using System.Collections.Generic;
class Supplier
{
public string Name;
public Supplier (string name)
{
Name = name;
}
}
class Program
{
static void Main()
{
List<Supplierlist = new List<Supplier>();
list.Add (new Supplier("Fred"));
list.Add (new Supplier("Joe"));
Check(list, "Fred");
Check(list, "Bob");
}
static void Check(List<Supplierlist, string nameToCheck)
{
Console.WriteLine (list.Exists
(
delegate(Supplier x) { return x.Name == nameToCheck; }
));
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too