Skip to content

Commit babd9f5

Browse files
committed
Add lazy implementation
1 parent 7f2ba90 commit babd9f5

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Threading;
2+
3+
#if NET35
4+
namespace System
5+
{
6+
internal class Lazy<T>
7+
{
8+
private readonly Func<T> _valueFactory;
9+
private readonly object _lock;
10+
11+
private T? _value;
12+
private bool _valueCreated;
13+
14+
public Lazy(Func<T> valueFactory, LazyThreadSafetyMode mode)
15+
{
16+
_valueFactory = valueFactory;
17+
_lock = new object();
18+
}
19+
20+
public T Value
21+
{
22+
get
23+
{
24+
lock (_lock)
25+
{
26+
if (_valueCreated)
27+
{
28+
return _value!;
29+
}
30+
31+
_value = _valueFactory();
32+
_valueCreated = true;
33+
return _value;
34+
}
35+
}
36+
}
37+
38+
}
39+
}
40+
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#if NET35
2+
namespace System.Threading
3+
{
4+
internal enum LazyThreadSafetyMode
5+
{
6+
None,
7+
PublicationOnly,
8+
ExecutionAndPublication,
9+
}
10+
}
11+
#endif

0 commit comments

Comments
 (0)