Re: Help with Generic LIst
Code:
List<string> list = new List<string>();
list.Add("Hello World");
if (list.Contains("Hello World"))
{
MessageBox.Show("List says Hello");
}
else
{
MessageBox.Show("List doesn't say Hello");
}
if (list.Contains("Goodbye Cruel World"))
{
MessageBox.Show("List says Goodbye");
}
else
{
MessageBox.Show("List doesn't say Goodbye");
}
Exists() is more complex. Predicate is a delegate. If you want to know more then read the help topics for the Exists method and the Predicate delegate. It's all there in the MSDN library, including code examples.
Re: Help with Generic LIst
I have read about the Contains Methods in my MSDN but i have not found an answer on how it perform its searching whether it search for the item exactly as the paramater you provide or search for an item that has something like the parameter. for example, i have 3 items of type string in my list named my_list. "Hello World","HI ALL","ITS GREAT TO BE HERE" does
Code:
my_list.Contains("Hello");
will return true since of the items contains "Hello"?
Re: Help with Generic LIst
The Contains method tells you whether the collection contains the exact item that you specify. If you want to perfrom a fuzzier search then the Exists method is the way to go, but your first post didn't really indicate that that was what you wanted. When you call Exists you must pass a Predicate, which basically means a method reference with the same signature as the Predicate delegate. The Predicate delegate has a return type of 'bool' and has a single argument of type T, which means the same type as the items in your List. If you're using a List<string> then the method you're calling must return a bool and take a single string parameter. That method is then called once for each item in the List, with the item being passed to the method argument. If the method returns False every time then Exists returns False. If the method returns True once then it is not called for any more items. Try this code as an example:
Code:
private string substring;
private void Form1_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Hello World");
list.Add("Hi There");
list.Add("Goodbye Cruel World");
this.substring = "world";
if (list.Exists(ContainsSubstring))
{
MessageBox.Show("List contains 'world'");
}
else
{
MessageBox.Show("List does not contain 'world'");
}
this.substring = "goodbye";
if (list.Exists(ContainsSubstring))
{
MessageBox.Show("List contains 'goodbye'");
}
else
{
MessageBox.Show("List does not contain 'goodbye'");
}
this.substring = "pineapple";
if (list.Exists(ContainsSubstring))
{
MessageBox.Show("List contains 'pineapple'");
}
else
{
MessageBox.Show("List does not contain 'pineapple'");
}
}
private bool ContainsSubstring(string item)
{
MessageBox.Show("Item: " + item + "; Substring: " + this.substring);
// Check whether the current item contains the specified substring.
return (item.IndexOf(this.substring, StringComparison.CurrentCultureIgnoreCase) != -1);
}
Re: Help with Generic LIst
Note that to show where the Predicate delegate comes in, this:
Code:
if (list.Exists(ContainsSubstring))
is actually shorthand for this:
Code:
if (list.Exists(new Predicate<string>(ContainsSubstring)))
Re: Help with Generic LIst
Thanks for the example Jm..it really gives me an idea what a predicate is and how to use it for List..i'll try that as soon as i get to work..Thanks again.