472,986 Members | 2,890 Online

# Predicates explained: three ways to declare predicates

Inspired by Jon, I did a demo prorgram showing three ways to declare
predicates, in for example the "FindIndex" and "FindLastIndex" methods
of Lists, but in general you can do this for any predicate.

Note the most compact way is to use Lambda notation and anonymous
functions, the first way shown below. The most 'intuitive' way is to
'hard code' the predicate using a predicate method, the second way
shown below, but the best way (for me) is to use an idea Goran gave me
and that is to construct a predicate class that 'generalizes' the hard
codes value you are using in your predicate. This is shown in the
third way below.

If you study this example you will have mastered predicates, which are
used everywhere, for example in Array.Find as well.

RL

(gives a simplistic example, that really does not do justice to the
topic, as is typical for MSDN)

// September 11, 2008
//OUTPUT:

/*
0 1 2 3 4 5 6 7 8 -10

lastIndex is: 8, firstIndex is 5
lastIndex2 is: 8
firstIndex2 is: 5
lastIndex3 is: 8
firstIndex3 is: 5
Press any key to continue . . .

*/

namespace Console1
{
class Program
{
static void Main(string[] args)
{

List<intnewList = new List<int>();

for (int i = 0; i < 10; i++)
{
}
newList.Remove(9);
//note: commenting out this above line gives: 0 1 2 3 4 5 6 7 8 -10 9
(ten elements) since nothing removed!

newList.Insert(9, -10); //gives output 0 1 2 3 4 5 6 7 8
-10

foreach (int i in newList)
{
Console.Write(" {0}", i);
}

Console.WriteLine("\n");
int y = 5; //the value we want as the comparison point

int lastIndex = newList.FindLastIndex(value =value >=
y);
int firstIndex = newList.FindIndex(value =value >= y);

//the above way uses anonymous functions and 'lambda' notation and is
very compact as you can tell

Console.WriteLine("lastIndex is: {0}, firstIndex is {1}",
lastIndex, firstIndex); //last index is 8 (i.e. List[8] = 8) first
index is 5 (List[5]= 5)

// now the second 'hard coded' way--note y>=5 is 'hard coded' into the
predicate method

int lastIndex2 = newList.FindLastIndex(MyPredicate);
Console.WriteLine("lastIndex2 is: {0}", lastIndex2);
int firstIndex2 = newList.Find(MyPredicate);
Console.WriteLine("firstIndex2 is: {0}", firstIndex2);

//now the neatest (IMO) way: the predicate class way (verbose but
easy to understand and flexible)

int lastIndex3 = newList.FindLastIndex(new
PredicateClass(y).MyPredicate);
Console.WriteLine("lastIndex3 is: {0}", lastIndex3);
int firstIndex3 = newList.Find(new
PredicateClass(y).MyPredicate);
Console.WriteLine("firstIndex3 is: {0}", firstIndex3);

}
private static bool MyPredicate(int value)
{
if (value >= 5) //value “hard coded” here –compare with
other versions
{ return true ; }
else
{ return false; }

}

//third way: construct a predicate class, so you can pick any y easily

public class PredicateClass
{
private int _y;
public PredicateClass(int y)
{
_y = y;
}
public bool MyPredicate (int value)
{
if (value >= _y)
{return true;}
else
{return false;}
}

}

Sep 10 '08 #1
0 1366

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 1 by: jim | last post by: I have two tables that are related by keys. For instance, Table employee { last_name char(40) not null, first_name char(40) not null, department_name char(40) not null, age int not null, ...... 2 by: Sugapablo | last post by: Can anyone help me out with some code to change three table cells (
) when one is hovered over? I have a calendar grid where each day is made up of three table cells and I want all three to... 2 by: Thong Nguyen | last post by: I wrote my own Predicate class for .NET 1.1 which allowed composite predicates using operator overloading... for example: Predicate p1 = {...}; Predicate p2 = {...}; Predicate p1andp2 =... 0 by: dayzman | last post by: Hi, M'm writing a program to output the results of transforming logical predicates using deMorgan's. Does anyone know of a library that handles logical predicates? Cheers, Michael 2 by: Praveen | last post by: Hi, Hi I am a newbie in DB2 and Mainframe.I would like to know if there is any diffence between the Quantified Predicates: IN , ANY, and SOME. Thank you very much for your valuable time. ... 9 by: Yitzak | last post by: Hi spent a few hours on this one wrote a query that joined on results of 2 other queries. Qry3 using Qry1 and Qry2 When I used Qry1.FasText