Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#

Using Find in a Generic List

Rate me:
Please Sign up or sign in to vote.
4.06/5 (7 votes)
27 Oct 2008CPOL3 min read 70.8K   999   33  
This article investigates the use of List.Find() when using value and reference types, and how to use it when working with your own custom classes.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GenericsFind
{
    public partial class frmMain : Form
    {
        private List<int> intList1;
        private List<Nullable<int>> intListNullable1;
        Person[] pArray;
        List<Person> PersonList;

        public frmMain()
        {
            InitializeComponent();
            KickOff();
        }

        private void KickOff()
        {
            // populate the lists of value types
            intList1 = new List<int>(new int[] { 1, 2, 3, 4 });
            intListNullable1 = new List<int?>(new int?[] { 1, 2, 3, 4 });

            pArray = new Person[] { new Person("John Smith"), new Person("Victor Matfield") };
            PersonList = new List<Person>(new Person[] { new Person("John Smith"), new Person("Victor Matfield") });
        }

        private void btnFindValueType_Click(object sender, EventArgs e)
        {
            try
            {
                int intInput1 = System.Convert.ToInt32(txtValueInput1.Text.ToString(), 10);

                int intFound = intList1.Find(delegate(int intpar1) { return intpar1 == intInput1; });

                txtValueResponse.Text = "FOUND! : " + intFound + " was found in {1,2,3,4}";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                txtValueInput1.Text = "";
            }            
        }

        private void btnFindValueTypeNullable_Click(object sender, EventArgs e)
        {
            try
            {
                int intInput1 = System.Convert.ToInt32(txtValueInput1.Text.ToString(), 10);

                int? intFound = intListNullable1.Find(delegate(int? intpar1) { return intpar1 == intInput1; });

                if (intFound.HasValue)
                {
                    txtValueResponse.Text = "FOUND : value " + intFound + " was found in {1,2,3,4}";
                }
                else
                {
                    txtValueResponse.Text = "FAIL : value " + intFound + " was NOT found in {1,2,3,4}";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                txtValueInput1.Text = "";
            }
        }

        private void btnFindPerson_Click(object sender, EventArgs e)
        {
            try
            {
                string strInput1 = txtRerenceInput1.Text;

                // bool blresult = PersonList.Exists(delegate(Person pTempSearch) { return pTempSearch.Equals(new Person(strInput1)); });
                Person pFound = PersonList.Find(delegate(Person pTempSearch) { return pTempSearch.Equals(new Person(strInput1)); });

                if (pFound != null)
                {
                    txtReferenceResponse.Text = "FOUND : person " + strInput1 + " was found in list";
                }
                else
                {
                    txtReferenceResponse.Text = "FAIL : person " + strInput1 + " was NOT found in list";
                }                               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                txtRerenceInput1.Text = "";
            }
        }

        private void btnFindArray_Click(object sender, EventArgs e)
        {
            try
            {
                string strInput1 = txtRerenceInput1.Text;

                Person pInput = new Person(strInput1);

                bool blResult = ArrayContains<Person>(pArray, pInput);

                if (blResult)
                {
                    txtReferenceResponse.Text = "FOUND : person " + strInput1 + " was found in list";
                }
                else
                {
                    txtReferenceResponse.Text = "FAIL : person " + strInput1 + " was NOT found in list";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                txtRerenceInput1.Text = "";
            }
        } 

        public bool ArrayContains<T>(T[] array, T value)
        {
            return array != null &&

            Array.Exists(array, delegate(T tTemp)
            {
                return tTemp.Equals(value);
            });
        }           
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
South Africa South Africa
I'm a senior software developer from South Africa and have a huge passion for .NET and Microsoft related technologies.

When I'm not having fun figuring out an architecture or class design, you'd probably find me rocking on the drums, running around with my badminton racquet or enjoying another bitter coffee or an ice-cold Amstel at the bar.

Comments and Discussions