有没有办法指定“空”C# lambda 表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1743013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to specify an "empty" C# lambda expression?
提问by Rob
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?
我想声明一个“空”的 lambda 表达式,它什么都不做。有没有办法在不需要方法的情况下做这样的事情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. 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:
我这样做的目的只是控制我的 WPF 命令的启用/禁用状态,但这是旁观。也许这对我来说太早了,但我想必须有一种方法可以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.
有没有办法做到这一点?似乎没有必要采取什么都不做的方法。
采纳答案by Rauhotz
Action doNothing = () => { };
回答by Jon Skeet
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 节。)
回答by Joseph
This should work:
这应该有效:
SomeMenuCommand = new RelayCommand(
x => {},
x => CanSomeMenuCommandExecute());
回答by Jorge Córdoba
I don't fully understand why do you need a DoNothing method.
我不完全明白为什么你需要一个 DoNothing 方法。
Can't you just do:
你不能只做:
SomeMenuCommand = new RelayCommand(
null,
x => CanSomeMenuCommandExecute());
回答by Anthony
This is an old question, but 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);