Can you explain to me:
A predicate is a function that returns true
or false
. A predicate delegate is a reference to a predicate.
So basically a predicate delegate is a reference to a function that returns true
or false
. Predicates are very useful for filtering a list of values - here is an example.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int> { 1, 2, 3 };
Predicate<int> predicate = new Predicate<int>(greaterThanTwo);
List<int> newList = list.FindAll(predicate);
}
static bool greaterThanTwo(int arg)
{
return arg > 2;
}
}
Now if you are using C# 3 you can use a lambda to represent the predicate in a cleaner fashion:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int> { 1, 2, 3 };
List<int> newList = list.FindAll(i => i > 2);
}
}
If you're in VB 9 (VS2008), a predicate can be a complex function:
Dim list As New List(Of Integer)(New Integer() {1, 2, 3})
Dim newList = list.FindAll(AddressOf GreaterThanTwo)
...
Function GreaterThanTwo(ByVal item As Integer) As Boolean
'do some work'
Return item > 2
End Function
Or you can write your predicate as a lambda, as long as it's only one expression:
Dim list As New List(Of Integer)(New Integer() {1, 2, 3})
Dim newList = list.FindAll(Function(item) item > 2)
Predicate falls under the category of generic delegates in C#. This is called with one argument and always return the boolean type. Basically, the predicate is used to test the condition - true/false. Many classes support predicate as an argument. For example, list.findall expects the parameter predicate. Here is an example of predicate.
Imagine a function pointer with the signature:
<modifier> bool delegate myDelegate<in T>(T match);
Here is the example:
Node.cs:
namespace PredicateExample
{
class Node
{
public string Ip_Address { get; set; }
public string Node_Name { get; set; }
public uint Node_Area { get; set; }
}
}
Main class:
using System;
using System.Threading;
using System.Collections.Generic;
namespace PredicateExample
{
class Program
{
static void Main(string[] args)
{
Predicate<Node> backboneArea = Node => Node.Node_Area == 0 ;
List<Node> Nodes = new List<Node>();
Nodes.Add(new Node { Ip_Address = "1.1.1.1", Node_Area = 0, Node_Name = "Node1" });
Nodes.Add(new Node { Ip_Address = "2.2.2.2", Node_Area = 1, Node_Name = "Node2" });
Nodes.Add(new Node { Ip_Address = "3.3.3.3", Node_Area = 2, Node_Name = "Node3" });
Nodes.Add(new Node { Ip_Address = "4.4.4.4", Node_Area = 0, Node_Name = "Node4" });
Nodes.Add(new Node { Ip_Address = "5.5.5.5", Node_Area = 1, Node_Name = "Node5" });
Nodes.Add(new Node { Ip_Address = "6.6.6.6", Node_Area = 0, Node_Name = "Node6" });
Nodes.Add(new Node { Ip_Address = "7.7.7.7", Node_Area = 2, Node_Name = "Node7" });
foreach( var item in Nodes.FindAll(backboneArea))
{
Console.WriteLine("Node Name " + item.Node_Name + " Node IP Address " + item.Ip_Address);
}
Console.ReadLine();
}
}
}
Leading on from Andrew's answer with regards to c#2 and c#3 ... you can also do them inline for a one off search function (see below).
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int> { 1, 2, 3 };
List<int> newList = list.FindAll(delegate(int arg)
{
return arg> 2;
});
}
}
Hope this helps.