-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lazy.dib
79 lines (61 loc) · 1.38 KB
/
Lazy.dib
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
#!markdown
# Lazy data type
#!csharp
class Person
{
public string Name { get; set;}
public string LastName { get; set;}
}
var p = new Person() { Name = "John", LastName = "Smith" };
var pl = new Lazy<Person>(() => new Person() { Name = "John", LastName = "Smith" });
display(pl);
display(pl.Value);
display(pl);
#!csharp
class SlowPerson
{
public string Name { get; set;}
public string LastName { get; set;}
public SlowPerson()
{
System.Threading.Thread.Sleep(1000);
}
}
#!csharp
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
var globalPerson = new SlowPerson() { Name = "John", LastName = "Smith" };
void Processing(bool usePerson)
{
if (usePerson)
{
Console.WriteLine(globalPerson.LastName);
}
else
{
Console.WriteLine("No need for person");
}
}
Processing(usePerson: false);
//Processing(usePerson: true);
sw.Stop();
display(sw.Elapsed);
#!csharp
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
var globalPersonLazy = new Lazy<SlowPerson>(() => new SlowPerson() { Name = "John", LastName = "Smith" });
void Processing(bool usePerson)
{
if (usePerson)
{
Console.WriteLine(globalPersonLazy.Value.LastName);
}
else
{
Console.WriteLine("No need for person");
}
}
Processing(usePerson: false);
//Processing(usePerson: true);
sw.Stop();
display(sw.Elapsed);