PDA

View Full Version : Management with collection c



elecit2008
دوشنبه 14 بهمن 1387, 22:12 عصر
.NET Collection Management
with C# 3.0

by Amirthalingam Prasanna
Using C# 3.0 to manage a collection of objects

Generics in C#, enable you to define classes, interfaces, delegates or methods with placeholders for parameterized
types used within. This allows you to define classes that use a generic type, and define the type at the time of
instantiation or method calls. This makes your code strongly typed, but makes maintenance easier. Prasanna
describes the improvements in .NET v3.5. This article looks into some of the new features in C# 3.0 and
introduces Linq in managing a collection of objects within a generic List.

Introduction

A couple of years ago, I wrote an article entitled “.NET Collection Management” for Simple-Talk. The purpose
of that article was to introduce generics and to show how generics can be used to manage a collection of
strongly typed objects within a generic List using C# 2.0. Generics allow us to write code without binding
the code to a particular type, and at the same time ensures we can use strongly typed objects. I thought I’d
revisit the article to see how much my code would be simplified and improved in C# 3.0, and introduce
some of the new features in C# 3.0.
Let us define an Employee class that we will be using throughout the examples in this article. The Employee
class has the properties Name and Salary.

public class Employee
{
public string Name { get; set; }
public double Salary { get; set; }
}

We have omitted the implementation of the properties because their implementation is very simple. You
set a value to a private field and get the value from a private field. So we are going to let the compiler
implement the properties for us. This is a feature called “Automatic Properties” that saves a few lines of
code and improves the readability of the code when the property implementation is very simple.
Next we will define and use a collection of Employee objects using a List<T>.

List<Employee> col = new List<Employee>();
col.Add(new Employee() { Name = "John", Salary = 25500 });
col.Add(new Employee() { Name = "Smith", Salary = 32000 });

In this code, we have used a special syntax in initializing the property values at the time of creating Employee
objects. This is a feature called “Property Initialization” that provides a very easy way of initializing one or
more properties when creating an object.

Sorting a List

We can use the Comparison delegate and pass it into the Sort method of List<Employee>. In the following
code we will use an anonymous method to pass the instance of a Comparison delegate to do the sorting
operation. The anonymous method simplifies the call to the Sort method since we do not need to define a
separate method.

col.Sort(delegate(Employee emp1,Employee emp2)
{
return emp1.Salary.CompareTo(emp2.Salary);
});

The Developers Group Magazine March/April 2008 Page 2 return to index

We could have written this code in C# 2.0. But in C# 3.0 we can further simplify the implementation by
using Lambda expressions for method implementations. Lambda expression is an inline method
implementation that is translated to an instance of a delegate by the compiler. These expressions use the
syntax “(parameter 1, parameter 2 …) => method implementation”. Lambda expressions allow us to define
methods on the fly with a simpler syntax compared to anonymous methods. So the above code can be
simplified by using the Lambda expression syntax as follows:

col.Sort((emp1, emp2) => emp1.Salary.CompareTo(emp2.Salary));

By using the Lambda expression, I have omitted defining the type for emp1 and emp2. Since the Sort method
accepts an instance of a Comparison delegate for Employee objects, the compiler is intelligent enough to
understand that emp1 and emp2 has to refer to Employee objects. The expression “(emp1, emp2) =>
emp1.Salary.CompareTo(emp2.Salary)” will be translated to an instance of the Comparison delegate.
Another way of sorting the generic List is by using the static method Enumerable.OrderBy. This method will
return an ordered collection of Employee objects

IEnumerable<Employee> orderedEmp = Enumerable.OrderBy<Employee, double>(col, (emp) =>
emp.Salary);

The OrderBy method is an extension method. An “Extension method” is a new feature in C# 3.0 that allows
you to call a static method belonging to a class as if it is an instance method belonging to an object. This also
allows us to extend types which normally we might not be able to extend. So the OrderBy method can be
called as if it is an instance method because it is an extension method. The compiler would replace it as a call
to the static Enumerable.OrderBy extension method:
IEnumerable<Employee> orderedEmp = col.OrderBy<Employee,double>((emp) => emp.Salary);