C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Collections »

Use of Predicate delegate in Find() method of generic List<>


Posted Date: 17 Sep 2009    Resource Type: Code Snippets    Category: Collections
Author: Vikram Singh KshatriyaMember Level: Gold    
Rating: 1 out of 5Points: 5



Using the generic List clollection might create a problem with the List.Contains() method.If a condition is placed to check if the generic List contains some string then it returns 'true' only if the string which is passed to "Contains()" method is in in same case as stored in the list.
For example, if List contains a string value "TEST" and we call the List.Contains("Test") then it returns "false" as it treats "name" and "Name" different values even they are same.
To overcome this problem, we can use "Find()" method of a generic List and pass a delegate which will compare the case sensitiveness of string values.


using System.Collections.Generic;
class TestList
{
public void CheckValueInList(List stringList)
{
if(stringList != null)
{
string result = stringList.Find(new Predicate(IsEqualString);
}
}

public static bool IsEqualString(string name)
{
return (name.Equals("NAME",
StringComparison.OrdinalIgnoreCase));
}


}


By using the Predicate delegate with the generic List we get the return value as "True" as delegte invokes the associated method to check string comparison.In this way, we can overcome the limitation of "Contains()" method.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Using Find method of generic List<>  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Use System.Collection.Generic method to Load Dropdownlist
Previous Resource: A Really Funny VB Script
Return to Discussion Resource Index
Post New Resource
Category: Collections


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use