是否有可能在C#中的lambda表达式使用可选/默认参数? [英] Is it possible to use optional/default parameters in a lambda expression in c#?

查看:819
本文介绍了是否有可能在C#中的lambda表达式使用可选/默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有用的可选参数(默认参数)的方式= http://msdn.microsoft.com/en-us/library/bb397687.aspx\">lambda表达式在C#中的?我已经通过文件读取,但可以发现没什么可说的一种方式或其他。

Is there a way to use optional arguments (default parameters) with lambda expressions in c#? I have read through the documentation but can find nothing to say one way or the other.

要说明这一点,我可以定义使用可选参数提供一个默认的简单方法值像这样:

To illustrate, I can define simple method that uses an optional argument to supply a default value like so:

void MyMethod(string arg = "default-value")
{ 
    Console.WriteLine(arg);
}



我想知道的是,如果我能够用做同样的事情。lambda表达式

What I want to know is if I am able to do the same thing using a lambda expression.

// gives a syntax error
Action<string> MyMethod = (arg = "default") => Console.WriteLine(arg);



我可以用委托的默认值的可选参数,但是这似乎有点笨拙

I can work in an optional parameter with a default value using a delegate, but this seems a bit clumsy.

delegate void MyDelegate(string arg = "default");
MyDelegate MyMethod = arg => Console.WriteLine(arg);



我也可以在身体的lambda检查参数,类似...

Alternatively I could check the parameter in the lambda body, something like...

 Action<string> MyMethod = (arg) => Console.WriteLine(string.IsNullOrEmpty(arg) ?
                                    "default" :
                                    arg);



但同样,这似乎有点笨拙。

But again this seems a bit clumsy.

时有可能使用可选参数的lambda表达式在C#中设置的默认值?

Is it possible to use optional parameters to set a default value in a lambda expression in c#?

推荐答案

没有。主叫方(代码调用代表)没有发现lambda表达式,所以也没有意义的有指定默认参数。所有来电者看到的是委托。在你的情况,例如,调用代码的只有的知道动作<串> - 如何是为了了解编译器提供的默认值这是由lambda表达式?

No. The caller (the code invoking the delegate) doesn't "see" the lambda expression, so it doesn't make sense to specify the default parameter there. All the caller sees is the delegate. In your case, for example, the calling code only knows about Action<string> - how is the compiler meant to know to supply the default value that's specified by the lambda expression?

由于事情如何变得棘手的例子说明,想象一下,如果这的的可行性。然后,考虑下面的代码:

As an example of how things get tricky, imagine if this were viable. Then consider this code:

Action<string> action;
if (DateTime.Today.Day > 10)
{
    action = (string arg = "boo") => Console.WriteLine(arg); 
}
else
{
    action = (string arg = "hiss") Console.WriteLine(arg);
}
action(); // What would the compiler do here?



记住该参数是由编译器的在调用点<提供/ EM

Bear in mind that the argument is provided by the compiler at the call site - so what should it do with the final line?

这是一个有点像有一个接口和一个实现 - - >?如果你有一个接口上的默认参数,这很好;如果只有它的执行,那么只有谁知道具体的实施呼叫者会看到它。在lambda表达式的情况下,真的没有看到实施呼叫者使用。但只是委托签名

It's a bit like having an interface and an implementation - if you have a default parameter on an interface, that's fine; if you only have it on the implementation, then only callers who know the specific implementation will see it. In the case of lambda expressions, there's really no visible implementation for the caller to use: there's just the delegate signature.

这篇关于是否有可能在C#中的lambda表达式使用可选/默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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