Skip to content

Commit de75cbd

Browse files
authored
MemoryPool
1 parent ea3ef6f commit de75cbd

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

CsharpLangExamples.sln

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MulticastDelegates", "Multi
7575
EndProject
7676
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArrayPool", "ArrayPool\ArrayPool.csproj", "{FDB639A1-C301-4C4C-955F-DE77A2517176}"
7777
EndProject
78+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MemoryPool", "MemoryPool\MemoryPool.csproj", "{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}"
79+
EndProject
7880
Global
7981
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8082
Debug|Any CPU = Debug|Any CPU
@@ -225,6 +227,10 @@ Global
225227
{FDB639A1-C301-4C4C-955F-DE77A2517176}.Debug|Any CPU.Build.0 = Debug|Any CPU
226228
{FDB639A1-C301-4C4C-955F-DE77A2517176}.Release|Any CPU.ActiveCfg = Release|Any CPU
227229
{FDB639A1-C301-4C4C-955F-DE77A2517176}.Release|Any CPU.Build.0 = Release|Any CPU
230+
{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
231+
{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}.Debug|Any CPU.Build.0 = Debug|Any CPU
232+
{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}.Release|Any CPU.ActiveCfg = Release|Any CPU
233+
{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}.Release|Any CPU.Build.0 = Release|Any CPU
228234
EndGlobalSection
229235
GlobalSection(SolutionProperties) = preSolution
230236
HideSolutionNode = FALSE

MemoryPool/MemoryPool.csproj

+10
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>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

MemoryPool/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Buffers;
3+
4+
// MemoryPool<T> Class
5+
// https://learn.microsoft.com/en-us/dotnet/api/system.buffers.memorypool-1?view=net-8.0
6+
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Memory/src/System/Buffers/MemoryPool.cs
7+
8+
// Use the singleton pool
9+
// https://stackoverflow.com/questions/73661594/whats-the-point-of-c-sharp-memory-pool
10+
using var owner = MemoryPool<double>.Shared.Rent(100);
11+
Memory<double> mem = owner.Memory;
12+
13+
MemoryPool<byte> memoryPool = MemoryPool<byte>.Shared;
14+
IMemoryOwner<byte> memoryOwner = memoryPool.Rent(1024);
15+
Memory<byte> memory = memoryOwner.Memory;
16+
17+
try
18+
{
19+
Span<byte> span = memory.Span;
20+
span[0] = 42;
21+
}
22+
finally
23+
{
24+
memoryOwner.Dispose();
25+
}
26+

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Examples of C# programming, with the goal of tracking and staying up-to-date wit
5757
* [Dynamic types](DynamicTypes/)
5858
* [Multicast Delegates](MulticastDelegates/)
5959
* [ArrayPool](ArrayPool/)
60+
* [MemoryPool](MemoryPool/)
6061

6162
## Notes
6263
- [Which C# version is included in which framework version?](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version)

0 commit comments

Comments
 (0)