Search code examples
c#arrayspredicate

Difference between passing a Predicate and a method


I have a class Account, with a field representing the money on the account, and an array of accounts in the Main method. If I wanted to display all accounts with a non-negative balance, I would use the FindAll method in the class Array. I don't understand the difference between these two ways of doing that.

using System;

namespace Account_Predicate
{
    class Account
    {
        int money;

        public int Money { get => money; set => money = value; }
    }

    internal class Program
    {
        static void Main()
        {
            Account[] accounts = new Account[20];

            Random r = new Random();                    //
            for (int i = 0; i < accounts.Length; i++)   //
            {                                           //  fill the array with accounts with random amounts of money
                accounts[i] = new Account();            //
                accounts[i].Money = r.Next(-100, 100);  //
            }

            Display(accounts);

            Account[] results = Array.FindAll(accounts, FindNonNegative);

            Console.WriteLine("------------------");

            Display(results);

            Console.ReadKey();
        }

        static void Display(Account[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine(array[i].Money);
            }
        }

        static bool FindNonNegative(Account account)
        {
            return account.Money >= 0;
        }
    }
}

The other way:

static void Main()
        {
            Account[] accounts = new Account[20];

            Random r = new Random();                    //
            for (int i = 0; i < accounts.Length; i++)   //
            {                                           //  fill the array with accounts with random amounts of money
                accounts[i] = new Account();            //
                accounts[i].Money = r.Next(-100, 100);  //
            }

            Display(accounts);

            Predicate<Account> predicate = FindNonNegative;

            Account[] results = Array.FindAll(accounts, predicate);

            Console.WriteLine("------------------");

            Display(results);

            Console.ReadKey();
        }

Is there any difference between passing a method directly vs passing a predicate, and if there is why would one be used instead of the other one?


Solution

  • No real difference. In both cases a Predicate<Account> is getting created from the FindNonNegative reference via implicit cast. In the second example you're just saving that predicate into a variable first.

    Heinzi's example in the comment is very appropriate. I'll make a slight variation: in both of these cases, an int is being implicitly cast into an Object. In one, you're using a variable to hold that object.

    object i = 1; 
    Console.WriteLine(i);
    
    Console.WriteLine(1);