Home
  Latest posts
  My Writings
  My Code
  My Gallery
  About me
 
  rssfeed Syndication
 
Bloggtoppen.se
 
 
Links
  Cornerstone
  SweNug
 
Post categories
  misc (48)
  Architecture (21)
  C# (19)
  Asp.Net (2)
  Vb.Net (2)
  Training (7)
  Data (19)
  Events (40)
  Platform (2)
  Orcas (4)
  Updates (3)
  Methods (10)
  Tools (6)
  Announcements (14)
  Languages (1)
  Patterns (6)
  Opinions (11)
  Fun (3)
  Ineta (1)
  Opinion (0)
  Practices (2)
  WCF (5)
 
 
 

More C# 2.0 usages (generics, anonymous methods)

Wednesday, January 18, 2006

In an earlier post I talked about a simple scenario where we could use generics. In this post I'll expand a bit on that and look at the List class and what you can do with generics in cooperation with anonymous methods and delegates.

 

The generic List class is the successor of the ArrayList class. The former was weakly typed and could only store System.Object which led to runtime casting and boxing scenarios.

 

The List class helps us out here and creates a strongly typed list. I assume you're familiar with the basics of generics so consider the following code:

 

// See the attached download file for complete code.
public class Customer {
string Name;
string Company;
decimal YearlyOrderAmount;
}
.
.
.
List listOfCustomers = CustomerRepository.List();

Now we have a strongly typed list of customers in our application code. With the help of the generic type in the list, Microsoft has been able to create a couple of helpful methods on the List class. Methods that use the generic type together with generic delegates to give us some really nice features. A couple of thoose methods are listed below:

 

public bool Exists (Predicate match)
public
ListFindAll (Predicate match)
public T FindLast (
Predicate match)

The Predicate delegate is a simple delegate that points at methods that accepts one single parameter of the type T and returns a boolean. The definition of the Predicate delegate is as follows:

 

public delegate bool Predicate (T obj)

 

The methods above use this delegate to execute code that will decide if an object is a match or not.

 

Let's start off with a simple example on how to use it with regular delegate syntax:

 

Predicate matchDelegate = new Predicate(AllWithAInTheirName);
.
.
.
public bool AllWithEInTheirName(Customer customerToMatch)
{
return (customerToMatch.Name.ToLower().IndexOf("e") >= 0)
}

 

We can now use the delegate object in our FindAll method to filter out all Customers with an E in their name:

 

List customersWithAnA = listOfCustomers.FindAll(matchDelegate);

 

For every object in the list, the FindAll method will execute the predicate delegate and pass it the object to determine if it matched our criteria or not.

 

Now for the really dynamic bit :) How about using some anonymous delegates in this scenario?

 

List customersWithE = customerList.FindAll(
delegate(Customer customerToMatch)
{
return (customerToMatch.Name.ToLower().IndexOf("e") >= 0);
}
);

 

In this piece of code we use the C# 2.0 ability to create a method on the fly which helps us decide the rules of a match on the spot where it's needed. Although not the most beautiful code in the world, this is really flexible and allows us to quickly set up new filter options for other use cases without the need for creating methods and put them in a class somewhere.



Download the code here Predicate.Zip [1k]


 

Comments
6/21/2010 7:29:00 AM    https://lowendahl.net/showShout.aspx?id=6   -   Cygnet Infotech Pvt. Ltd
 
the guided format is very helpful which would definitely help with software programmers in building the programs for specified task


Comment
Title:
Your name:
Your url:
Text:
Please enter the text from the image: