开发者

Default parameter values in C#'s lambda expressions

开发者 https://www.devze.com 2023-01-20 14:39 出处:网络
Good afternoon, Can someone please tell me if I can set default parameter values when using lambda expressions in C#? For example, if I have the code

Good afternoon,

Can someone please tell me if I can set default parameter values when using lambda expressions in C#? For example, if I have the code

public stati开发者_运维百科c Func<String, Int32, IEnumerable<String>> SomeFunction = (StrTmp, IntTmp) => { ... },

how can I set IntTmp's default value to, for example, two? The usual way to set default parameter values in a method seems not to work with this kind of expressions (and I really need one of this kind here).

Thank you very much.


You really cannot unless you do it via composition of functions:

public static Func<String, Int32, IEnumerable<String>> SomeFunction = 
                                          (StrTmp, IntTmp) => { ... };

public static Func<String, IEnumerable<String>> SomeFunctionDefaulted =
                                  strTmp => SomeFunction(strTmp, 2);

You could also try modifying SomeFunction to take a nullable, but then you would have to explicitly pass null for a value and check for that in the method body.

0

精彩评论

暂无评论...
验证码 换一张
取 消