-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathLazy.cs
87 lines (80 loc) · 4.01 KB
/
Lazy.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Threading;
using JetBrains.Annotations;
namespace CodeJam
{
/// <summary>
/// Helper methods for <see cref="Lazy{T}"/> class.
/// </summary>
[PublicAPI]
public static class Lazy
{
/// <summary>
/// Initializes a new instance of the <see cref="Lazy{T}"/> class. When lazy initialization occurs, the default
/// constructor of the target type is used.
/// </summary>
/// <typeparam name="T">The type of object that is being lazily initialized.</typeparam>
/// <returns>New <see cref="Lazy{T}"/> instance.</returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static Lazy<T> Create<T>() => new();
/// <summary>
/// Initializes a new instance of the <see cref="Lazy{T}"/> class. When lazy initialization occurs, the default
/// constructor of the target type is used.
/// </summary>
/// <typeparam name="T">The type of object that is being lazily initialized.</typeparam>
/// <param name="isThreadSafe">
/// <c>true</c> to make this instance usable concurrently by multiple threads; <c>false</c> to make the instance
/// usable by only one thread at a time.
/// </param>
/// <returns>New <see cref="Lazy{T}"/> instance.</returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static Lazy<T> Create<T>(bool isThreadSafe) => new(isThreadSafe);
/// <summary>
/// Initializes a new instance of the <see cref="Lazy{T}"/> class. When lazy initialization occurs, the default
/// constructor of the target type is used.
/// </summary>
/// <typeparam name="T">The type of object that is being lazily initialized.</typeparam>
/// <param name="valueFactory">
/// The delegate that is invoked to produce the lazily initialized value when it is needed.
/// </param>
/// <returns>New <see cref="Lazy{T}"/> instance.</returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static Lazy<T> Create<T>(Func<T> valueFactory) => new(valueFactory);
/// <summary>
/// Initializes a new instance of the <see cref="Lazy{T}"/> class. When lazy initialization occurs, the default
/// constructor of the target type is used.
/// </summary>
/// <typeparam name="T">The type of object that is being lazily initialized.</typeparam>
/// <param name="mode">One of the enumeration values that specifies the thread safety mode.</param>
/// <returns>New <see cref="Lazy{T}"/> instance.</returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static Lazy<T> Create<T>(LazyThreadSafetyMode mode) => new(mode);
/// <summary>
/// Initializes a new instance of the <see cref="Lazy{T}"/> class. When lazy initialization occurs, the default
/// constructor of the target type is used.
/// </summary>
/// <typeparam name="T">The type of object that is being lazily initialized.</typeparam>
/// <param name="valueFactory">
/// The delegate that is invoked to produce the lazily initialized value when it is needed.
/// </param>
/// <param name="isThreadSafe">
/// <c>true</c> to make this instance usable concurrently by multiple threads; <c>false</c> to make the instance
/// usable by only one thread at a time.
/// </param>
/// <returns>New <see cref="Lazy{T}"/> instance.</returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static Lazy<T> Create<T>(Func<T> valueFactory, bool isThreadSafe) => new(valueFactory, isThreadSafe);
/// <summary>
/// Initializes a new instance of the <see cref="Lazy{T}"/> class. When lazy initialization occurs, the default
/// constructor of the target type is used.
/// </summary>
/// <typeparam name="T">The type of object that is being lazily initialized.</typeparam>
/// <param name="valueFactory">
/// The delegate that is invoked to produce the lazily initialized value when it is needed.
/// </param>
/// <param name="mode">One of the enumeration values that specifies the thread safety mode.</param>
/// <returns>New <see cref="Lazy{T}"/> instance.</returns>
[Pure, System.Diagnostics.Contracts.Pure]
public static Lazy<T> Create<T>(Func<T> valueFactory, LazyThreadSafetyMode mode) => new(valueFactory, mode);
}
}