-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
45 lines (37 loc) · 1.23 KB
/
Program.cs
File metadata and controls
45 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// C# 14 – Exploring extension members
// https://devblogs.microsoft.com/dotnet/csharp-exploring-extension-members/
// Extension members
// https://learn.microsoft.com/en-gb/dotnet/csharp/whats-new/csharp-14#extension-members
// Extension members (C# Programming Guide)
// https://learn.microsoft.com/en-gb/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
// Extension declaration (C# Reference)
// https://learn.microsoft.com/en-gb/dotnet/csharp/language-reference/keywords/extension
var list = new List<int>() { 1, 2, 3 };
if (list.IsEmpty)
{
Console.WriteLine("list is empty");
return;
}
var empty = IEnumerable<int>.EmptySet;
public static class ListExtensions
{
// Block that adds new capabilities to the IEnumerable<T> type
extension<T>(IEnumerable<T> source)
{
// Extension Property
public bool IsEmpty => !source.Any();
// Extension Method
public void PrintAll()
{
foreach (var item in source)
{
Console.WriteLine(item);
}
}
}
// Static Extension (Called directly via type)
extension<T>(IEnumerable<T>)
{
public static IEnumerable<T> EmptySet => Enumerable.Empty<T>();
}
}