最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is the equivalent javascript closure in c#? - Stack Overflow

programmeradmin1浏览0评论

Consider this simple .js code:

const createCounter = () => {
    let value = 0;
    return {
        increment: () => { value += 1 },
        decrement: () => { value -= 1 },
        logValue: () => { console.log(value); }
    }
}

// Usage

const { increment, decrement, logValue } = createCounter();

I'm pretty sure c# support first class function, note that I don't want to use classes to remake the code above. What is the equivalent closure in c#?

I have made this:

public Func<WhatType?> CreateCounter = () => {
    var value = 0;
    return what?
}

Consider this simple .js code:

const createCounter = () => {
    let value = 0;
    return {
        increment: () => { value += 1 },
        decrement: () => { value -= 1 },
        logValue: () => { console.log(value); }
    }
}

// Usage

const { increment, decrement, logValue } = createCounter();

I'm pretty sure c# support first class function, note that I don't want to use classes to remake the code above. What is the equivalent closure in c#?

I have made this:

public Func<WhatType?> CreateCounter = () => {
    var value = 0;
    return what?
}
Share Improve this question edited Aug 15, 2019 at 15:24 GoldenAge 3,0766 gold badges28 silver badges65 bronze badges asked Aug 15, 2019 at 14:52 Kevin TanudjajaKevin Tanudjaja 8861 gold badge9 silver badges19 bronze badges 2
  • You could use a Map of functions. I don't know if there's such a thing as destructuring in C# (or similar) – MinusFour Commented Aug 15, 2019 at 15:01
  • could you provide any examples or references to Map of functions which will hold the value state? – Kevin Tanudjaja Commented Aug 16, 2019 at 3:29
Add a ment  | 

2 Answers 2

Reset to default 13

You could use a mix of ValueTuples and lambda expressions.

private static (Action increment, Action decrement, Action logValue) CreateCounter()
{
    var value = 0;

    return
        (
            () => value += 1,
            () => value -= 1,
            () => Console.WriteLine(value)
        );
}

Usage

var (increment, decrement, logValue) = CreateCounter();
increment();
increment();
decrement();
logValue();

Check out the following code using Dictionary to Map enumerated data types with an Action Delegate

void Main()
{
    OperationActionDictionary[Operation.Increment](); // Execute Increment
    OperationActionDictionary[Operation.Increment](); // Execute Increment
    OperationActionDictionary[Operation.Decrement](); // Execute Decrement
    OperationActionDictionary[Operation.LogValue]();  // Execute LogValue
}

public enum Operation
{
    Increment,
    Decrement,
    LogValue
}

public static int Value = 0;

public Dictionary<Operation,Action> OperationActionDictionary = new Dictionary<Operation, Action>
{
    [Operation.Increment] = () => Value += 1,
    [Operation.Decrement] = () => Value -= 1,
    [Operation.LogValue] = () => Console.WriteLine($"Value :: {Value}")
};

Only catch here or in any other code for modifying a shared Value object would be in case it is Multi thread access, then you need to take care of thread safety in this case using Interlocked.Increment or Interlocked.Decrement

发布评论

评论列表(0)

  1. 暂无评论