有没有办法指定一个“空"?C# lambda 表达式? [英] Is there a way to specify an "empty" C# lambda expression?

查看:13
本文介绍了有没有办法指定一个“空"?C# lambda 表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想声明一个空"的 lambda 表达式,它什么都不做.有没有办法在不需要 DoNothing() 方法的情况下做这样的事情?

I'd like to declare an "empty" lambda expression that does, well, nothing. Is there a way to do something like this without needing the DoNothing() method?

public MyViewModel()
{
    SomeMenuCommand = new RelayCommand(
            x => DoNothing(),
            x => CanSomeMenuCommandExecute());
}

private void DoNothing()
{
}

private bool CanSomeMenuCommandExecute()
{
    // this depends on my mood
}

我这样做的目的只是控制我的 WPF 命令的启用/禁用状态,但这是一个问题.也许对我来说早上太早了,但我想必须有一种方法来声明 x =>DoNothing() lambda 表达式以某种方式完成同样的事情:

My intent in doing this is only control the enabled/disabled state of my WPF command, but that's an aside. Maybe it's just too early in the morning for me, but I imagine there must be a way to just declare the x => DoNothing() lambda expression in some way like this to accomplish the same thing:

SomeMenuCommand = new RelayCommand(
    x => (),
    x => CanSomeMenuCommandExecute());

有没有办法做到这一点?似乎没有必要采取什么都不做的方法.

Is there some way to do this? It just seems unnecessary to need a do-nothing method.

推荐答案

Action doNothing = () => { };

这篇关于有没有办法指定一个“空"?C# lambda 表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆