The New Lambda Expressions Feature in C# 3.0 | CodeGuru

The New Lambda Expressions Feature in C# 3.0

C# 2.0 (released in November 2005) introduced a new feature, anonymous methods, that allows you to declare your method code inline instead of with a delegate function. Lambda expressions, a new feature in C# 3.0, have a more concise syntax to achieve the same goal. Take a closer look at anonymous methods before discussing lambda […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 12, 2006
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

C# 2.0 (released in November 2005) introduced a new feature, anonymous methods, that allows you to declare your method code inline instead of with a delegate function. Lambda expressions, a new feature in C# 3.0, have a more concise syntax to achieve the same goal. Take a closer look at anonymous methods before discussing lambda expressions.

Suppose you want to create a button that updates a ListBox on your form when you click it. In C# 1.0 and 1.1, you would do it as follows:

public MyForm()
{
   listBox   = new ListBox(...);
   textBox   = new TextBox(...);
   addButton = new Button(...);
   addButton.Click += new EventHandler(AddClick);
}

void AddClick(object sender, EventArgs e)
{
   listBox.Items.Add(textBox.Text);
}

With C# 2.0, you could have it as under:

public MyForm()
{
   listBox   = new ListBox(...);
   textBox   = new TextBox(...);
   addButton = new Button(...);
   addButton.Click += delegate
   {
      listBox.Items.Add(textBox.Text);
   };

As you can see, you don’t have to explicitly declare a new method to link it with an event. You can use anonymous methods to achieve the same thing in C# 2.0. C# 3.0 introduces an even simpler syntax, lambda expressions, which you write as a parameter list followed by the “=>” token, followed by an expression or a statement block.

Parameters to Lambda Expressions

The parameters of a lambda expression can be explicitly or implicitly typed. In an explicitly typed parameter list, the type of each expression is explicitly specified. In an implicitly typed parameter list, the types are inferred from the context in which the lambda expression occurs:

(int x) => x + 1          // explicitly typed parameter

(y,z) => return y * z;    // implicitly typed parameter

Lambda Expressions Demonstration

The following code snippet from the downloadable source code uses two techniques to print out strings in a list whose length is even. The first technique, implemented in the function AnonMethod, is through anonymous methods. The second technique, implemented in the function LambdaExample, is through a lambda expression:

// Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;

namespace LambdaExample
{
   public delegate bool KeyValueFilter<K, V>(K key, V value);
   static class Program
   {

      static void Main(string[] args)
      {
         List<string> list = new List<string>();

         list.Add("AA");
         list.Add("ABC");
         list.Add("DEFG");
         list.Add("XYZ");
         Console.WriteLine("Through Anonymous method");
         AnonMethod(list);
         Console.WriteLine("Through Lambda expression");
         LambdaExample(list);

         Dictionary<string, int> varClothes= new Dictionary<string,int>();

         varClothes.Add("Jeans", 20);
         varClothes.Add("Shirts", 15);
         varClothes.Add("Pajamas", 9);
         varClothes.Add("Shoes", 9);
         var ClothesListShortage = varClothes.FilterBy((string name,
            int count) => name == "Shoes" && count < 10);
            // example of multiple parameters
         if(ClothesListShortage.Count > 0)
            Console.WriteLine("We are short of shoes");
         Console.ReadLine();
      }

      static void AnonMethod(List<string> list)
      {


         List<string> evenNumbers =
            list.FindAll(delegate(string i)
            { return (i.Length % 2) == 0; });

         foreach (string evenNumber in evenNumbers)
         {
            Console.WriteLine(evenNumber);

         }
      }

      static void LambdaExample(List<string> list)
      {
         var evenNumbers = list.FindAll(i =>
            (i.Length % 2) == 0);    // example of single parameter
         foreach(string i in evenNumbers)
         {
            Console.WriteLine(i);
         }
      }
   }

   public static class Extensions
   {
      public static Dictionary<K, V> FilterBy<K, V>
         (this Dictionary<K, V> items, KeyValueFilter<K, V> filter)
      {
         var result = new Dictionary<K, V>();

         foreach(KeyValuePair<K, V> element in items)
         {
            if (filter(element.Key, element.Value))
               result.Add(element.Key, element.Value);
         }

         return result;
      }

   }
}

The second technique is sort of interesting. It filters the list of strings based on the length of the string, but it uses a new syntax. If you have Visual Studio 2005 and LinQ Preview installed, you can use the editor to compile the application. If you do not have VS2005, you can compile the code from the command line by using the following command:

C:Program FilesLINQ PreviewBinCsc.exe
/reference:"C:Program FilesLINQ PreviewBinSystem.Data.DLinq.dll"
/reference:C:WINDOWSMicrosoft.NETFrameworkv2.0.50727System.Data.dll
/reference:C:WINDOWSMicrosoft.NETFrameworkv2.0.50727System.dll
/reference:"C:Program FilesLINQ PreviewBinSystem.Query.dll"
/reference:C:WINDOWSMicrosoft.NETFrameworkv2.0.50727System.Xml.dll
/reference:"C:Program FilesLINQ PreviewBinSystem.Xml.XLinq.dll"
/target:exe Program.cs
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.