blob: 2f08a24a97929a7a401f4da7f410284aa840a7ae (
plain)
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
51
52
53
54
|
using System;
using System.Diagnostics;
namespace LibGit2Sharp.Core.Compat
{
/// <summary>
/// Provides support for lazy initialization.
/// </summary>
/// <typeparam name="TType">Specifies the type of object that is being lazily initialized.</typeparam>
[DebuggerStepThrough]
public class Lazy<TType>
{
private readonly Func<TType> evaluator;
private TType value;
private bool hasBeenEvaluated;
private readonly object padLock = new object();
/// <summary>
/// Initializes a new instance of the <see cref="Lazy{TType}"/> class.
/// </summary>
/// <param name="evaluator">The <see cref="Func{TResult}"/> that will be called to evaluate the value of this Lazy instance.</param>
public Lazy(Func<TType> evaluator)
{
Ensure.ArgumentNotNull(evaluator, "evaluator");
this.evaluator = evaluator;
}
/// <summary>
/// Gets the lazily initialized value of the current instance.
/// </summary>
public TType Value
{
get { return Evaluate(); }
}
private TType Evaluate()
{
if (!hasBeenEvaluated)
{
lock (padLock)
{
if (!hasBeenEvaluated)
{
value = evaluator();
hasBeenEvaluated = true;
}
}
}
return value;
}
}
}
|