-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArrayPoolSingletonDesign.cs
More file actions
50 lines (45 loc) · 1.02 KB
/
ArrayPoolSingletonDesign.cs
File metadata and controls
50 lines (45 loc) · 1.02 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
46
47
48
49
50
void Main()
{
InvokeLicencePool();
ReinvokeLicencePool();
}
sealed class SingletonArrayPool<T>
{
private static readonly Lazy<ArrayPool<T>> _singleton =
new Lazy<ArrayPool<T>>(() => ArrayPool<T>.Shared);
private SingletonArrayPool() { }
public static ArrayPool<T> Shared => _singleton.Value;
}
public record Licence
{
public int Id { get; init; }
public string HairColor { get; init; }
public string EyeColor { get; init; }
public ushort WeightInPound { get; init; }
public byte Age { get; init; }
// 20 more properties ...
}
public void InvokeLicencePool()
{
var licencePool = SingletonArrayPool<Licence>.Shared;
var licenceArr = licencePool.Rent(10);
for (int i = 0; i < 10; i++)
{
licenceArr[i] = new()
{
Id = 0,
EyeColor = "Black",
HairColor = "Black",
WeightInPound = 155,
Age = 33
};
}
licenceArr.Dump();
licencePool.Return(licenceArr);
}
public void ReinvokeLicencePool()
{
var licencePool = SingletonArrayPool<Licence>.Shared;
var licenceArr = licencePool.Rent(10);
licenceArr.Dump();
}