Return a lambda function in CSharp

Description

The following code shows how to return a lambda function.

Example


   /*www  .  j  a va  2  s .  c  o m*/
using System;
delegate int NumericSequence();

class Test {
    static NumericSequence Natural() {
        int seed = 0;
        return () => seed++;

    }

    static void Main() {
        NumericSequence natural = Natural();
        Console.WriteLine(natural());
        Console.WriteLine(natural());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor