System.Predicate for the System.Collections.Generic.List<string>Exists function

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

Abhi

In the following hypothetical example I want to build a generic list
of unique string items.
How should I implement the pred function so that it returns true/false
if target string exists in the generic list...is not clear to me. Any
suggestions?

System.Collections.Generic.List<string> list = new
System.Collections.Generic.List<string>();

foreach(string s in myAnotherArray)
{

System.Predicate<string> p = new System.Predicate<string> (pred);
if(!list.Exists(p))
{
list.Add(s);
}
}


protected bool pred(string target)
{
//TODO: How to find out target string exists in the generic
list????
}
 
Abhi,

Which list are you trying to check to see if the value exists?
Basically, you want to pass the predicate to the list in order to see if the
values that are already in the list meet a certain condition.

You might want to use anonymous delegates here, as they will allow you
to specify the condition much easier depending on what you have set up.

Can you give a code sample, as well as a more detailed explaination of
what the condition you are trying to check is?
 
you do something like this

foreach( string s in list2 )
{

bool found = list.Exists( delegate(string t) {
return s == t;
} );

if( !found) list.Add( s );

}
 
Hello back Nicholas-

I actually needed to use the "Contains" function ...but due to my
ignorance, I mistook the "Exists" function for that job.

list.Contains("mystring") is what I needed for checking to see if a
string exists in my generic list. Pardon me for the confusion in the
code snippet in my original posting. It was a lame attempt to translate
my original issue into a more "simplistic" representation of the
problem at hand.
Thanks!
 
Back
Top