简体   繁体   English

有没有办法指定“空” C# lambda 表达式?

[英]Is there a way to specify an "empty" C# lambda expression?

I'd like to declare an "empty" lambda expression that does, well, nothing.我想声明一个“空的” lambda 表达式,嗯,什么都不做。 Is there a way to do something like this without needing the DoNothing() method?有没有办法在不需要DoNothing()方法的情况下做这样的事情?

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.我这样做的目的只是控制我的 WPF 命令的启用/禁用 state,但这是一个旁白。 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:也许现在对我来说还太早,但我想一定有一种方法可以像这样以某种方式声明x => DoNothing() lambda 表达式来完成同样的事情:

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 = () => { };

I thought I would add some code that I've found useful for this type of situation.我想我会添加一些我发现对这种情况有用的代码。 I have an Actions static class and a Functions static class with some basic functions in them:我有一个Actions静态类和一个Functions静态类,其中包含一些基本功能:

public static class Actions
{
  public static void Empty() { }
  public static void Empty<T>(T value) { }
  public static void Empty<T1, T2>(T1 value1, T2 value2) { }
  /* Put as many overloads as you want */
}

public static class Functions
{
  public static T Identity<T>(T value) { return value; }

  public static T0 Default<T0>() { return default(T0); }
  public static T0 Default<T1, T0>(T1 value1) { return default(T0); }
  /* Put as many overloads as you want */

  /* Some other potential methods */
  public static bool IsNull<T>(T entity) where T : class { return entity == null; }
  public static bool IsNonNull<T>(T entity) where T : class { return entity != null; }

  /* Put as many overloads for True and False as you want */
  public static bool True<T>(T entity) { return true; }
  public static bool False<T>(T entity) { return false; }
}

I believe this helps improve readability just a tiny bit:我相信这有助于提高可读性只是一点点:

SomeMenuCommand = new RelayCommand(
        Actions.Empty,
        x => CanSomeMenuCommandExecute());

// Another example:
var lOrderedStrings = GetCollectionOfStrings().OrderBy(Functions.Identity);

This should work:这应该有效:

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

Assuming you only need a delegate (rather than an expression tree) then this should work:假设您只需要一个委托(而不是表达式树),那么这应该可以工作:

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

(That won't work with expression trees as it's got a statement body . See section 4.6 of the C# 3.0 spec for more details.) (这不适用于表达式树,因为它有一个语句体。有关更多详细信息,请参阅 C# 3.0 规范的第 4.6 节。)

I don't fully understand why do you need a DoNothing method.我不完全明白为什么你需要一个 DoNothing 方法。

Can't you just do:你不能只做:

SomeMenuCommand = new RelayCommand(
                null,
                x => CanSomeMenuCommandExecute());
Action DoNothing = delegate { };
Action DoNothing2 = () => {};

I used to initialize Events to a do nothing action so it was not null and if it was called without subscription it would default to the 'do nothing function' instead of a null-pointer exception.我曾经将 Events 初始化为一个什么都不做的动作,所以它不是空的,如果它在没有订阅的情况下被调用,它将默认为“不做任何功能”而不是空指针异常。

public event EventHandler<MyHandlerInfo> MyHandlerInfo = delegate { };

Starting with C# 9.0 you can specify discards _ for required parameters.从 C# 9.0 开始,您可以为所需参数指定丢弃_ Example:例子:

Action<int, string, DateTime> action = (_, _, _) => { };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM