Skip to content

Commit 6524102

Browse files
dotnet 7 list patterns sample
1 parent e42d995 commit 6524102

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.32916.344
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ListPatterns", "ListPatterns.csproj", "{3134C4B4-0C1B-49F8-A513-6888046A328C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3134C4B4-0C1B-49F8-A513-6888046A328C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3134C4B4-0C1B-49F8-A513-6888046A328C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3134C4B4-0C1B-49F8-A513-6888046A328C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3134C4B4-0C1B-49F8-A513-6888046A328C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {F02BD99B-15C7-4D7F-946F-7CB6947228C0}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Numerics;
2+
3+
int[] list1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
4+
double[] list2 = { 1, 2, 3, 4.4, 5, 6, 7, 8, 9 };
5+
6+
var result = AddAll1(list1);
7+
Console.WriteLine(result);
8+
9+
var result2 = AddAll2(list2);
10+
Console.WriteLine(result2);
11+
12+
var result3 = AddAll3(list2);
13+
Console.WriteLine(result3);
14+
15+
var result4 = AddAll4(list2.AsSpan());
16+
Console.WriteLine(result4);
17+
18+
int AddAll1(int[] values)
19+
{
20+
int result = 0;
21+
foreach (var value in values)
22+
{
23+
result += value;
24+
}
25+
return result;
26+
}
27+
28+
// with INumber<T> constraint!
29+
T AddAll2<T>(T[] values) where T : INumber<T>
30+
{
31+
T result = T.Zero;
32+
foreach (var value in values)
33+
{
34+
result += value;
35+
}
36+
return result;
37+
}
38+
39+
// with list pattern matching - and INumber<T> constraint
40+
T AddAll3<T>(T[] values) where T : INumber<T> =>
41+
values switch
42+
{
43+
[] => T.Zero,
44+
[var first, .. var rest] => first + AddAll3(rest),
45+
};
46+
47+
T AddAll4<T>(Span<T> values) where T : INumber<T> =>
48+
values switch
49+
{
50+
[] => T.Zero,
51+
[var first, .. var rest] => first + AddAll4(rest),
52+
};

0 commit comments

Comments
 (0)