DotNETWeekly-io / DotNetWeekly

DotNet weekly newsletter
MIT License
198 stars 3 forks source link

【文章推荐】Lambda 参数默认值 #685

Closed gaufung closed 1 day ago

gaufung commented 2 weeks ago

https://devblogs.microsoft.com/dotnet/refactor-your-code-with-default-lambda-parameters/

gaufung commented 5 days ago

C# 12 之后, Lambda 表达式的参数可以使用默认参数。举例来说,在 C# 12 之前,我们的 lambda 定义如下

// Before C# 12
var IncrementBy = static (int source, int? increment) => 
{
    return source + (increment ?? 1);
};

那么现在可以这样使用

var IncrementBy = static (int source, int increment = 1) => 
{
    return source + increment;
};