Storage Informer
Storage Informer

Use lambda expressions in C# to simplify the parallelized code

by on May.13, 2009, under Storage

Use lambda expressions in C# to simplify the parallelized code

So, you want to start using Task Parallel Library Beta 1. You want to take advantage of the new features that will be available in .Net 4.0. Hold on! Are you familiar with lambda expressions?

If you aren・t using lambda expressions in your current C# programs, you should begin learning about them before jumping into the new features. C# 3.0 introduced lambda expressions and they are very useful to simplify the parallelized code. They will help you to create code that・s easier to read, understand and maintain.

To keep things simple, a lambda expression is an anonymous function that can contain expressions and statements. It can be used to create delegates or expression tree types.

They are really useful to simplify the code when we use delegates. Parallelized code uses many delegates. Hence, lambda expressions allow you to simplify the code, reduce the complexity and understand what you・re doing. Parallelized code is more complex than serial code. Lambda expressions will help you to reduce this additional complexity.

All lambda expressions use the lambda operator =>. It is read as :goes to;.

For example, the following lines of code use the classic C# 2.0 syntax but they work with Parallel.ForEach (introduced in the Task Parallel Library):

System.Threading.Tasks.Task.Create(delegate(object myObject) {
try
{
Parallel.ForEach(myMeshes, delegate(Mesh myNextMesh)
{

Now, let・s take a look at the same code, but using lambda expressions:

System.Threading.Tasks.Task.Create(myObject =>
{
try
{
Parallel.ForEach(myMeshes, myNextMesh =>
{

The code is simpler, easier to read, understand and maintain.

Disclaimer: In the aforementioned lines of code, I・m using :System.Threading.Tasks.Task; because Task is new in the Task Parallel Library. A using statement could simplify the code even further.

The code creates a new task to run a parallelized loop asynchronously. It is an example. This is an advanced option. It is recommended for developers who understand what they are doing. I・m not recommending this practice to use in all the parallel loops. Some applications don・t need this kind of configurations.

Take into account that most developers will be using lambda expressions for parallelized code. By the way, most developers working with parallelized code using current features found in C# 3.0 and .Net 3.5 are also using lambda expressions.

Therefore, if you want to be able to understand parallelized code running in tasks and threads, you should begin working with lambda expressions.

URL: http://feedproxy.google.com/~r/IntelBlogs/~3/cNDqnoHFUoM/

:, , , , ,

Leave a Reply

Powered by WP Hashcash

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...