Skip to content

Commit 3a9b168

Browse files
committed
Extension declaration
1 parent 828db9e commit 3a9b168

5 files changed

Lines changed: 65 additions & 10 deletions

File tree

CsharpLangExamples.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParamsCollections", "Params
6161
EndProject
6262
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewEscapeSequence", "NewEscapeSequence\NewEscapeSequence.csproj", "{0138AED7-CAB5-4DBB-AEDF-9D933041FA14}"
6363
EndProject
64-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionTypesCsharp13", "ExtensionTypesCsharp13\ExtensionTypesCsharp13.csproj", "{0602BF99-2E7F-443B-BB8B-DBB869450FF9}"
65-
EndProject
6664
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheLockStatement", "TheLockStatement\TheLockStatement.csproj", "{739A4E7D-E8B8-4B4A-A199-5C839C6D312C}"
6765
EndProject
6866
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserDefinedConversionOperators", "UserDefinedConversionOperators\UserDefinedConversionOperators.csproj", "{CCD3ED3C-6C34-4F9E-A422-79479625FC75}"
@@ -101,6 +99,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyedServices", "KeyedServi
10199
EndProject
102100
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConcurrentCollections", "ConcurrentCollections\ConcurrentCollections.csproj", "{E40B458B-5337-47D9-8FC1-2C5167485271}"
103101
EndProject
102+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionTypesCsharp14", "ExtensionTypesCsharp14\ExtensionTypesCsharp14.csproj", "{0BDCEFD3-F484-4718-9E50-005842BB16F3}"
103+
EndProject
104104
Global
105105
GlobalSection(SolutionConfigurationPlatforms) = preSolution
106106
Debug|Any CPU = Debug|Any CPU
@@ -223,10 +223,6 @@ Global
223223
{0138AED7-CAB5-4DBB-AEDF-9D933041FA14}.Debug|Any CPU.Build.0 = Debug|Any CPU
224224
{0138AED7-CAB5-4DBB-AEDF-9D933041FA14}.Release|Any CPU.ActiveCfg = Release|Any CPU
225225
{0138AED7-CAB5-4DBB-AEDF-9D933041FA14}.Release|Any CPU.Build.0 = Release|Any CPU
226-
{0602BF99-2E7F-443B-BB8B-DBB869450FF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
227-
{0602BF99-2E7F-443B-BB8B-DBB869450FF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
228-
{0602BF99-2E7F-443B-BB8B-DBB869450FF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
229-
{0602BF99-2E7F-443B-BB8B-DBB869450FF9}.Release|Any CPU.Build.0 = Release|Any CPU
230226
{739A4E7D-E8B8-4B4A-A199-5C839C6D312C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
231227
{739A4E7D-E8B8-4B4A-A199-5C839C6D312C}.Debug|Any CPU.Build.0 = Debug|Any CPU
232228
{739A4E7D-E8B8-4B4A-A199-5C839C6D312C}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -295,6 +291,10 @@ Global
295291
{E40B458B-5337-47D9-8FC1-2C5167485271}.Debug|Any CPU.Build.0 = Debug|Any CPU
296292
{E40B458B-5337-47D9-8FC1-2C5167485271}.Release|Any CPU.ActiveCfg = Release|Any CPU
297293
{E40B458B-5337-47D9-8FC1-2C5167485271}.Release|Any CPU.Build.0 = Release|Any CPU
294+
{0BDCEFD3-F484-4718-9E50-005842BB16F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
295+
{0BDCEFD3-F484-4718-9E50-005842BB16F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
296+
{0BDCEFD3-F484-4718-9E50-005842BB16F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
297+
{0BDCEFD3-F484-4718-9E50-005842BB16F3}.Release|Any CPU.Build.0 = Release|Any CPU
298298
EndGlobalSection
299299
GlobalSection(SolutionProperties) = preSolution
300300
HideSolutionNode = FALSE
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>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

ExtensionTypesCsharp14/Program.cs

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

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Here are examples of C# programming to help you stay up-to-date with the latest
1212

1313
### C# 14 is supported on .NET 10 (November 2025)
1414

15-
| Feature | Description |
16-
|---------|-------------|
17-
| | |
15+
| Feature | Description |
16+
|--------------------------------------------------------|-------------|
17+
| [Extension members](ExtensionTypesCsharp14/Program.cs) | |
1818

1919
### C# 13 is supported on .NET 9 (September 2024)
2020

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "8.0.0",
3+
"version": "10.0.0",
44
"rollForward": "latestMajor",
55
"allowPrerelease": true
66
}

0 commit comments

Comments
 (0)