.NET Framework supported empty action syntax or singleton

折月煮酒 提交于 2019-12-02 01:33:21

There's nothing built-in that I'm aware of.

You could just define the delegate once as a helper singleton:

var anObject = new Foo(NoOpAction.Instance);

// ...

var anotherObject = new Bar(NoOpAction.Instance);

// ...

public static class NoOpAction
{
    private static readonly Action _instance = () => {};

    public static Action Instance
    {
        get { return _instance; }
    }
}

And because you're handed exactly the same delegate every time you use NoOpAction.Instance throughout your program, you're also saving on the (admittedly small) cost of creating and garbage-collecting multiple delegates that all do the same thing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!