Skip to content

Commit 15095cc

Browse files
committed
Default values for parameters in lambda expressions (csharp-12.0)
1 parent e1a748d commit 15095cc

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

CsharpLangExamples.sln

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StaticAbstractMembersInInte
5757
EndProject
5858
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrimaryConstructors", "PrimaryConstructors\PrimaryConstructors.csproj", "{161EE9F6-1074-4FAE-A38F-9871EE5BEDBF}"
5959
EndProject
60+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DefaultLambdaParameters", "DefaultLambdaParameters\DefaultLambdaParameters.csproj", "{14F8F02B-3066-408D-9330-25C54E49FD73}"
61+
EndProject
6062
Global
6163
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6264
Debug|Any CPU = Debug|Any CPU
@@ -171,6 +173,10 @@ Global
171173
{161EE9F6-1074-4FAE-A38F-9871EE5BEDBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
172174
{161EE9F6-1074-4FAE-A38F-9871EE5BEDBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
173175
{161EE9F6-1074-4FAE-A38F-9871EE5BEDBF}.Release|Any CPU.Build.0 = Release|Any CPU
176+
{14F8F02B-3066-408D-9330-25C54E49FD73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
177+
{14F8F02B-3066-408D-9330-25C54E49FD73}.Debug|Any CPU.Build.0 = Debug|Any CPU
178+
{14F8F02B-3066-408D-9330-25C54E49FD73}.Release|Any CPU.ActiveCfg = Release|Any CPU
179+
{14F8F02B-3066-408D-9330-25C54E49FD73}.Release|Any CPU.Build.0 = Release|Any CPU
174180
EndGlobalSection
175181
GlobalSection(SolutionProperties) = preSolution
176182
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<LangVersion>preview</LangVersion>
9+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10+
</PropertyGroup>
11+
12+
</Project>

DefaultLambdaParameters/Program.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+

2+
// Default values for parameters in lambda expressions
3+
// https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#default-lambda-parameters
4+
5+
var incrementValue = (int source, int increment) => source + increment;
6+
var incrementValueDefaultParameters = (int source, int increment = 1) => source + increment;
7+
8+
Console.WriteLine(incrementValue(5, 3)); // 8
9+
Console.WriteLine(incrementValue(5, 2)); // 7
10+
Console.WriteLine(incrementValueDefaultParameters(5)); // 6
11+
12+
var addition = (int val1, int val2) => val1 + val2;
13+
var sum = (params int[] values) =>
14+
{
15+
int sum = 0;
16+
foreach (var value in values)
17+
{
18+
sum = addition(sum, value);
19+
}
20+
21+
return sum;
22+
};
23+
24+
var empty = sum();
25+
Console.WriteLine(empty); // 0
26+
27+
var sequence = new[] { 1, 2, 3, 4, 5 };
28+
var total = sum(sequence);
29+
30+
Console.WriteLine(total); // 15
31+

0 commit comments

Comments
 (0)