467,894 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,894 developers. It's quick & easy.

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

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????
}

Nov 17 '05 #1
  • viewed: 14342
Share:
3 Replies
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?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Abhi" <ku******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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????
}

Nov 17 '05 #2
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 );

}

"Abhi" wrote:
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????
}

Nov 17 '05 #3
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!

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by s | last post: by
11 posts views Thread by Martin Jørgensen | last post: by
3 posts views Thread by banangroda | last post: by
42 posts views Thread by barcaroller | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.