C# IntelliBits: Delegates, Anonymous Types and Lambda Expressions

In this installment of C# IntelliBits, we will look at defining, creating and manipulating delegate types, anonymous types and lambda expressions in the C# language. Check out my last post for some basic C# tips.

Note: C# IntelliBits 1, 2 and 3 are covered in C# IntelliBits: Three Tips to Improve Your C# Knowledge.

C# IntelliBits #4: Delegates

Applies to: .NET Framework versions 1.1, 2.0, 3.0, 3.5, 4.0, 4.5 Under the .NET platform, the delegate type is the preferred means of defining and responding to callbacks within applications. In its simplest form, the .NET delegate type is a type-safe object that "points to" a method or a list of methods that can be invoked at a later time. Specifically, a delegate maintains three important pieces of information:

  • The address of the method on which it makes calls
  • The parameters (if any) of this method
  • The return type (if any) of this method

Note: .NET delegates can point to either static or instance methods.

Defining a Delegate Type in C#

To create a delegate type in C#, you use the delegate keyword. The name of your delegate type can be whatever you prefer. The only requirement is that the delegate must be defined to match the signature of the method(s) it will point to. Assume we build a delegate type named BinaryOperator that can point to any method that returns an integer and takes two integers as input parameters: // Takes two integer parameters and returns an integer. public delegate int BinaryOperator (int firstNum, int secondNum);

Delegate Example:

Sometimes there can be confusion when beginning to work with delegates. To get started we will look at a simple console application that makes use of the IntegerOperator delegate that was defined previously.

Code Sample:

namespace SimpleDelegate
  // takes two integers and returns an integer.
  public delegate int BinaryOperator(int firstNumber, int secondNumber);

  // This class contains methods BinaryOperator will point to.
  public class SimpleMath
    public static int Add(int firstNumber, int secondNumber)
    { return firstNumber + secondNumber; }

    public static int Subtract(int firstNumber, int secondNumber)
    { return firstNumber - secondNumber; }

  class Program
    static void Main(string[] args)
      Console.WriteLine("*****Delegate Example *****\n");

      // Create a BinaryOperator delegate object that
      // "points to" SimpleMath.Add().
      BinaryOperator binaryOperator = new BinaryOperator(SimpleMath.Add);

      // Invoke Add() method indirectly using delegate object.
      Console.WriteLine("10 + 10 is {0}", binaryOperator (10, 10));

C# IntelliBits #5: Anonymous Types

Applies to: .NET Framework versions 3.0, 3.5, 4.0, 4.5 Anonymous types are used to create an object without declaring its data type. Since the name of the data type is not specified, the type is referred to as an anonymous type.

Code Sample:

var Employee = new {FirstName = "Phylliss", LastName = "Daniels",
   DOH = DateTime.Now,EMPCode = 150};

Console.WriteLine(Employee.FirstName + Employee.LastName 
   + Employee.DOH + Employee.EmpCode );

C# IntelliBits #6: Lambda Expressions

Applies to: .NET Framework versions 3.0, 3.5, 4.0, 4.5 A lambda expression is an anonymous function. It is mostly used to create delegates in LINQ (I will cover LINQ in the future) and is a method without a declaration, access modifier, return value declaration or name. Lambda expressions allow you to write a method in the same location that it will be used. It’s useful in places where a method is only going to be used once. Lambda basic definition: Parameters => Executed code.

Code Sample:

x => x % 2 == 1 
x is the input parameter
x % 2 == 1 is the expression

You can read x => x % 2 == 1 like: "input parameter named x goes to anonymous function which returns true if the input is odd".


In this installment of C# IntelliBits, we covered delegates, anonymous type and lambda expressions. Next, I’ll write about the new features available in C# 5.0. Leave any C# questions you have in the comments.