是否有指定的方式"空" C#的lambda前pression? [英] Is there a way to specify an "empty" C# lambda expression?

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

问题描述

我想申报一个空拉姆达前pression,做,好了,什么都没有。
有没有办法做这样的事情,而不需要在 DoNothing()方法?

 公共MyViewModel()
    {
        SomeMenuCommand =新RelayCommand(
                X =>没做什么(),
                X => CanSomeMenuCommandExecute());
    }    私人无效DoNothing()
    {
    }    私人布尔CanSomeMenuCommandExecute()
    {
        //这个要看我的心情
    }

我的意图在做这个只是我的控制命令WPF的启用/禁用状态,但是这是题外话。也许这是在上午我只想太早,但我想一定有,只是声明办法X => DoNothing()拉姆达前$在一些像这样的方式来完成同样的事情p $ pssion:

  SomeMenuCommand =新RelayCommand(
        X => (),
        X => CanSomeMenuCommandExecute());

有没有一些方法来做到这一点?它只是似乎没有必要需要一个什么都不做的方法。


解决方案

 行动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
    }

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前pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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