C#: List contains custom object

Posted on 2008-10-01
Last Modified: 2013-12-17
Is there any way of using System.Collections.List.Contains() method to check if a collection of specific custom object has a given object, given that only one or few property (ies) of the object to look for are taken into account?
Question by:karakav
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 22613152
LVL 14

Expert Comment

ID: 22613201
use System.Collections.List.Find()  to implement custom search
try this sample...
public class TestObject
	public TestObject()
		System.Collections.Generic.List<testCalss> lstTestClass = new List<testCalss>();
		lstTestClass.Add(new testCalss(10 , 20));
		lstTestClass.Add(new testCalss(10 , 20));
		lstTestClass.Add(new testCalss(15 , 20));
		testCalss ObjClassFound;
		ObjClassFound = lstTestClass.Find(findObject);
	public bool findObject(testCalss val)
		if (val.iNumber1 == 15)
			return true;
			return false;
public class testCalss
	public Int32 iNumber1;
	public Int32 iNumber2;
	public testCalss(Int32 _iNumber1,Int32 _iNumber2)
		this.iNumber1 = _iNumber1;
		this.iNumber2 = _iNumber2;

Open in new window


Author Comment

ID: 22613326
What if I want to pass the value to compare to in the findObject delegate?
LVL 37

Accepted Solution

gregoryyoung earned 500 total points
ID: 22615177
use an anonymous delegate or create a small wrapper class for your delegate.

There are plenty of examples of using anonymous delegates available on the web and the latter basically does the same thing by hand that the compiler does with an anonymous delegate.

class MyTestClassMatcher {
   private readonly int number1;
   private readonly int number2;

   public MyTestClassMatcher(int _number1, int _number2) {
       number1 = _number1;
       number2 = _number2;

   public bool IsMatch(testCalss obj) {
         return obj.Number1 == number1 && obj.Number2 == number2;

To use like in the example above

                lstTestClass.Add(new testCalss(10 , 20));
                lstTestClass.Add(new testCalss(10 , 20));
                lstTestClass.Add(new testCalss(15 , 20));
                MyClassFinder finder = new MyClassFinder(10, 20);
                ObjClassFound = lstTestClass.Find(finder.IsMatch);

to read more about how to use anonymous delegates for this check out

but basically it would be ...

public testClass SearchList(List<testClass>list, int value1, int value2) {
    return list.Find(delegate(testClass obj)
                                   return obj.number1 == value1 && obj.number2 == value2;



Author Closing Comment

ID: 31501912
Thanks a lot. This tip is really amazing.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PowerShell:  Use of subproperties in a Select statement 7 64
Send parameters via datatable ajax call 4 40
aspx ascx, c# 7 38
WPF and Powershell button hover change text 5 57
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question