Shahid Riaz Bhatti

if(my.work == “Interesting” || my.availableTime > my.workHours) { this.blog.Post();}

How to play with IList using Loop, Predicate deligate and Lamda expression

October 10
by Shahid Riaz Bhatti 10. October 2008 18:46

In this article I will cover some basic things which we encountered in our daily programming tasks. These tasks includes how to find values from IList. This kind of tasks are extremely easy. We know that in programming we finish a tasks by using different techniques. e.g. We can write a simple program which inserts, update and delete some record from the database. This application sounds very simple and it is a simple but different approaches can be adopted to complete this tasks. Like we can adopt layered architecture, can adopt any design patterns depending on the nature of the application or we can violate all these rules. Application development is not a big tasks but how the application is developed is important.

Today I am going to discuss that how can we play with IList in C#. Basically I am a C# developer andhaving background of VC++, so I am comfortable by giving the C# example, but any VB.Net developer can also take benifit from these example.

A common task in our daily programming is searching. Different developers used different ways for searching.

Suppose I have a string collection and I want to fing a particular item from that string collection. Now how can we achieve this tasks? The simple answer is that iterate through the string collection to find the required entry. That is right but does it mean that we dont have any other option.. The good news is that we have some other options too which are given below:

  • Loop (Not a professional approach in case of any class which is implimenting IList interface
  • Predicate delegate (A little known delegate but it makes searching easier and much cleaner)
  • Lamda Expression (Avaiable only in .Net 3.0 and .Net 3.5)

Loop Approach: In this approach we use for, foreach or any other loop.

Lets say I have a collection of string as follow:

            // Declare the string collection
            List<string> myStings = new List<string>();
            // Add values in the collection
            myStings.Add("Shahid");
            myStings.Add("Riaz");
            myStings.Add("Bhatti");
            myStings.Add("Ayaz Khan");
            myStings.Add("Salman");
            myStings.Add("Kamran");
            myStings.Add("Munir");
            myStings.Add("Zafar Iqbal");
            myStings.Add("Asim");
            myStings.Add("Zahid");
            myStings.Add("Sheikh Ahmad");

In the above lines I have declared List of type string and added names of some persons in it. Now I want to find all the names where the length of name is greater than four (4). Using the Loop techniques we will do it some thing like this:

            Console.WriteLine("Find names where length is greater than 4 using Loop approach\n");
            foreach (string strName in myStings)
            {
                    if(strName.Length>4)
                        System.Console.WriteLine(strName);
            }

Predicate delegate approach:

The task which we have just finished by using the Loop approach can be completed by using the predicate delegate approach. Predicate delegate are not well known delegate but they make searching easy and much cleaner. Predicate are basically just a simple user defined boolean condition which we can define in our code to sort through collection.

Consider I have the sampe string collection which we declared in the Loop approach. Now we want to fing all the name where length is greater than four (4).

Predicate<string> callBack = new Predicate<string>(IsLengthGreaterThanFour);
            List<string> Names = myStings.FindAll(callBack);
            // Display all names where length name length is greater than 4
            foreach (string strName in Names)
            {
                Console.WriteLine(strName + "\n");
            }

The user defined boolean function which we used in Predicate delegate is IsLengthGreaterThanfour and it is given below:

private static bool IsLengthGreaterThanFour(string name)
        {
            return (name.Length > 7);
        }

The above code is self explanatory.

Lamda Expression: We can use Loop and predicate delegate in our code even in .Net 3.0 and .net 3.5. But we have another feature in .Net 3.0 and 3.5 which is Lamda expression.

Note: At the moment I dont have .Net 2008 on my laptop. So I didn't wrote any Lamda expression example. But I will write very soon. 

NOTE:

Predicate delegate can be used for the different function of IList, which includes:

  • Find
  • FindAll
  • Exists and many more
I have attached a .cs file in this post. You can check the implimentation of IList function in this .cs file. The code is commented in the example. I will update this POST very soon with the implementation of lamda expression.
 

Regards,

Shahid Riaz Bhatti

Microsoft certified application developer

Predicate.cs (1.35 kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

C# | Visual Studio

RecentComments

Comment RSS

Most comments

supplynflshop supplynflshop
51 comments
tiffany-bracelets tiffany-bracelets
39 comments
AVI to iPad AVI to iPad
36 comments