Reference the local variables from lambda in CSharp

Description

The following code shows how to reference the local variables from lambda.

Example


   /*from   www. jav  a2s  .  com*/


using System;
delegate int NumericSequence();

class Test {
    static void Main() {
        int seed = 0;
        NumericSequence natural = () => seed++;
        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