Sunday, December 18, 2011

Predicate C#

What is a Predicate?

The predicate is just a generic delegate that is defined in the .net framework class library. In the framework library, it is defined as

public delegate bool Predicate<T>(T obj);

This delegate is used for searching purposes.

Predicate Example

The FindAll() method in List<T> expects an instance of the predicate as a parameter. In the example below I show you how to do that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace PlayLambda
    public class Person
        public string name;
        public int Age;


    class Program

        static void Main(string[] args)
            Person P1 = new Person();
   = "Mike";
            P1.Age = 37;

            Person P2 = new Person();
   = "Tommy";
            P2.Age = 13;

            Person P3 = new Person();
   = "Kim";
            P3.Age = 27;

            List<Person> People = new List<Person>();


            Predicate<Person> myPredicate = IsDrinkingAge;

                     //FindAll() expects an instance of the Predicate as a parameter
            foreach (Person p in People.FindAll(myPredicate))
                Console.WriteLine("This person can legally drink: " +;


        static bool IsDrinkingAge(Person p)
            //The signature of this method should match the signature of the predicate
            if (p.Age > 21)
                return true;
                return false;



Shown below is the output of this program

Wednesday, December 14, 2011

What is LINQ?

Traditionally C# (like C++, java etc) has been an imperative programming language. That means if you want to fetch data from a source or dataset, we have to give the compiler a detailed set of precise instructions. For instance, suppose I have a datatable that has information on all my employees. If I want to find the salary of an employee named John, I would have to write code to loop thru every row in the datatable.
 Now suppose the same table is there in a SQL database. If I need to find the salary of John, all I would do is write something like

Select salary from tblEmployees where firstName='John'

As you can notice, in SQL I don't have to write code to loop through all the rows in the table.  DBMS does the job of looping through for me. This way of coding where you just declare what you want and the job gets done behind the scene by some engine for you, is called declarative programming.

 So what we have learned above can be summed up as

Imperative programming:  :  Give detailed steps to get required data. eg C#,C++ etc
Declarative programming (functional programming)  :  Tell the computer what you need and it will be fetched for you. eg SQL

Clearly, compared to imperative programming,  declarative programming requires a lot lesser code to retrieve data. LINQ is the way to write declarative code in C#.

Type Inference

In C# you can write

var x = "hello";

Since "hello" is a string, x becomes a string at compile time. This is called type inference. (As in, the type of variable x got inferred.)

If you try something like
var x = "hello";
int b = a;

The compiler will throw an error: Cannot implicitly convert type 'int' to 'string'.