FindAll() : Finding multiple items in C# List

Let us say we have a list of integer and we want to find all the number greater than 100.

If list is as follow

List<int> lst = new List<int>();
            lst.Add(20);
            lst.Add(300);
            lst.Add(400);
            lst.Add(9);
            lst.Add(19);
            lst.Add(789);
            lst.Add(45);

Now if we print this list

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> lst = new List<int>();
            lst.Add(20);
            lst.Add(300);
            lst.Add(400);
            lst.Add(9);
            lst.Add(19);
            lst.Add(789);
            lst.Add(45);
            foreach(var r in lst)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);

clip_image002

Now we need to find the entire element in the list greater than 100. So for this purpose we will use FindAll()

clip_image003

We can see that FindAll() takes a predicate. So we need to create a predicate and that takes an integer as input parameter.


public static bool GreaterThanHun(int value)
        {
            if (value > 100)
                return true;
            else
                return false;
        }

So when we now call this predicate as input parameter of FindAll() we will get the desired result of list numbers greater than 100.

clip_image005

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> lst = new List<int>();
            lst.Add(20);
            lst.Add(300);
            lst.Add(400);
            lst.Add(9);
            lst.Add(19);
            lst.Add(789);
            lst.Add(45);
            foreach(var r in lst)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);
            List<int> lstgrthund = lst.FindAll(GreaterThanHun);
            foreach (var r in lstgrthund)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);
        }
        public static bool GreaterThanHun(int value)
        {
            if (value > 100)
                return true;
            else
                return false;
        }
    }
}


Output

clip_image007

3 responses to “FindAll() : Finding multiple items in C# List”

  1. public static bool GreaterThanHun(int value)
    {
    if (value > 100)
    return true;
    else
    return false;
    }

    Hi dhananjay,

    Where is the ‘value’ parameter being passed in the program . The logic says if value is > 100 then return true . But the there is no “value” being passed when the function in being called in FINDALL

  2. Dhananjay Kumar

    Hi Praveen ,

    GreaterThanHun(int value) is a predicate

    what I mean to say that we are passing this as parameter to FindAll() , so runtime will take care of input paramter of predicate

    public delegate bool Predicate(
    T obj
    )

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com