-
Notifications
You must be signed in to change notification settings - Fork 0
/
LazyTest.cs
46 lines (41 loc) · 1.04 KB
/
LazyTest.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LazyTest
{
class Test
{
int[] _array;
public Test()
{
Console.WriteLine("Test()");
_array = new int[10];
}
public int Length
{
get
{
return _array.Length;
}
}
}
class Program
{
static void Main(string[] args)
{
// Create Lazy.
Lazy<Test> lazy = new Lazy<Test>();
// Show that IsValueCreated is false.
Console.WriteLine("IsValueCreated = {0}", lazy.IsValueCreated);
// Get the Value.
// ... This executes Test().
Test test = lazy.Value;
// Show the IsValueCreated is true.
Console.WriteLine("IsValueCreated = {0}", lazy.IsValueCreated);
// The object can be used.
Console.WriteLine("Length = {0}", test.Length);
}
}
}