|
1 | 1 | ---
|
2 | 2 | title: "How to: Perform Lazy Initialization of Objects"
|
3 | 3 | description: See how to perform lazy initialization of objects using the System.Lazy<T> class. Lazy initialization means objects aren't created if they're never needed.
|
4 |
| -ms.date: "03/30/2017" |
5 |
| -dev_langs: |
| 4 | +ms.date: 07/25/2025 |
| 5 | +dev_langs: |
6 | 6 | - "csharp"
|
7 | 7 | - "vb"
|
8 |
| -helpviewer_keywords: |
| 8 | +helpviewer_keywords: |
9 | 9 | - "lazy initialization in .NET, how to perform"
|
10 |
| -ms.assetid: 8cd68620-dcc3-4f20-8835-c728a6820e71 |
11 | 10 | ---
|
12 |
| -# How to: Perform Lazy Initialization of Objects |
| 11 | +# How to: Perform lazy initialization of objects |
| 12 | + |
| 13 | +The <xref:System.Lazy%601?displayProperty=nameWithType> class simplifies the work of performing lazy initialization and instantiation of objects. By initializing objects in a lazy manner, you can avoid having to create them at all if they're never needed, or you can postpone their initialization until they are first accessed. For more information, see [Lazy initialization](lazy-initialization.md). |
13 | 14 |
|
14 |
| -The <xref:System.Lazy%601?displayProperty=nameWithType> class simplifies the work of performing lazy initialization and instantiation of objects. By initializing objects in a lazy manner, you can avoid having to create them at all if they are never needed, or you can postpone their initialization until they are first accessed. For more information, see [Lazy Initialization](lazy-initialization.md). |
15 |
| - |
16 | 15 | ## Example 1
|
17 | 16 |
|
18 |
| - The following example shows how to initialize a value with <xref:System.Lazy%601>. Assume that the lazy variable might not be needed, depending on some other code that sets the `someCondition` variable to true or false. |
19 |
| - |
20 |
| -```vb |
21 |
| -Dim someCondition As Boolean = False |
22 |
| - |
23 |
| -Sub Main() |
24 |
| - 'Initializing a value with a big computation, computed in parallel |
25 |
| - Dim _data As Lazy(Of Integer) = New Lazy(Of Integer)(Function() |
26 |
| - Dim result = |
27 |
| - ParallelEnumerable.Range(0, 1000). |
28 |
| - Aggregate(Function(x, y) |
29 |
| - Return x + y |
30 |
| - End Function) |
31 |
| - Return result |
32 |
| - End Function) |
33 |
| - |
34 |
| - ' do work that may or may not set someCondition to True |
35 |
| - ' ... |
36 |
| - ' Initialize the data only if needed |
37 |
| - If someCondition = True Then |
38 |
| - |
39 |
| - If (_data.Value > 100) Then |
40 |
| - |
41 |
| - Console.WriteLine("Good data") |
42 |
| - End If |
43 |
| - End If |
44 |
| -End Sub |
45 |
| -``` |
46 |
| - |
47 |
| -```csharp |
| 17 | + The following example shows how to initialize a value with <xref:System.Lazy%601>. Assume that the lazy variable might not be needed, depending on some other code that sets the `someCondition` variable to `true` or `false`. |
| 18 | + |
| 19 | +```vb |
| 20 | +Dim someCondition As Boolean = False |
| 21 | + |
| 22 | +Sub Main() |
| 23 | + 'Initialize a value with a big computation, computed in parallel. |
| 24 | + Dim _data As Lazy(Of Integer) = New Lazy(Of Integer)(Function() |
| 25 | + Dim result = |
| 26 | + ParallelEnumerable.Range(0, 1000). |
| 27 | + Aggregate(Function(x, y) |
| 28 | + Return x + y |
| 29 | + End Function) |
| 30 | + Return result |
| 31 | + End Function) |
| 32 | + |
| 33 | + ' Do work that might or might not set someCondition to True... |
| 34 | + |
| 35 | + ' Initialize the data only if needed. |
| 36 | + If someCondition = True Then |
| 37 | + If (_data.Value > 100) Then |
| 38 | + Console.WriteLine("Good data") |
| 39 | + End If |
| 40 | + End If |
| 41 | +End Sub |
| 42 | +``` |
| 43 | + |
| 44 | +```csharp |
48 | 45 | static bool someCondition = false;
|
49 |
| - //Initializing a value with a big computation, computed in parallel |
50 |
| - Lazy<int> _data = new Lazy<int>(delegate |
51 |
| - { |
52 |
| - return ParallelEnumerable.Range(0, 1000). |
53 |
| - Select(i => Compute(i)).Aggregate((x,y) => x + y); |
54 |
| - }, LazyThreadSafetyMode.ExecutionAndPublication); |
55 |
| - |
56 |
| - // Do some work that may or may not set someCondition to true. |
57 |
| - // ... |
58 |
| - // Initialize the data only if necessary |
59 |
| - if (someCondition) |
60 |
| - { |
61 |
| - if (_data.Value > 100) |
62 |
| - { |
63 |
| - Console.WriteLine("Good data"); |
64 |
| - } |
65 |
| - } |
66 |
| -``` |
67 |
| - |
68 |
| -## Example 2 |
| 46 | + // Initialize a value with a big computation, computed in parallel. |
| 47 | + Lazy<int> _data = new Lazy<int>(delegate |
| 48 | + { |
| 49 | + return ParallelEnumerable.Range(0, 1000). |
| 50 | + Select(i => Compute(i)).Aggregate((x,y) => x + y); |
| 51 | + }, LazyThreadSafetyMode.ExecutionAndPublication); |
| 52 | + |
| 53 | + // Do some work that might or might not set someCondition to true... |
| 54 | +
|
| 55 | + // Initialize the data only if necessary. |
| 56 | + if (someCondition) |
| 57 | + { |
| 58 | + if (_data.Value > 100) |
| 59 | + { |
| 60 | + Console.WriteLine("Good data"); |
| 61 | + } |
| 62 | + } |
| 63 | +``` |
| 64 | + |
| 65 | +## Example 2 |
| 66 | + |
| 67 | +The following example shows how to use the <xref:System.Threading.ThreadLocal%601?displayProperty=nameWithType> class to initialize a type that is visible only to the current object instance on the current thread. |
| 68 | + |
| 69 | +[!code-csharp[CDS#13](../../../samples/snippets/csharp/VS_Snippets_Misc/cds/cs/cds2.cs#13)] |
| 70 | +[!code-vb[CDS#13](../../../samples/snippets/visualbasic/VS_Snippets_Misc/cds/vb/lazyhowto.vb#13)] |
69 | 71 |
|
70 |
| - The following example shows how to use the <xref:System.Threading.ThreadLocal%601?displayProperty=nameWithType> class to initialize a type that is visible only to the current object instance on the current thread. |
71 |
| - |
72 |
| - [!code-csharp[CDS#13](../../../samples/snippets/csharp/VS_Snippets_Misc/cds/cs/cds2.cs#13)] |
73 |
| - [!code-vb[CDS#13](../../../samples/snippets/visualbasic/VS_Snippets_Misc/cds/vb/lazyhowto.vb#13)] |
74 |
| - |
75 | 72 | ## See also
|
76 | 73 |
|
77 | 74 | - <xref:System.Threading.LazyInitializer?displayProperty=nameWithType>
|
|
0 commit comments