-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
77 lines (64 loc) · 2.11 KB
/
Program.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
namespace ReferenceTypes;
class Program
{
static void Main()
{
CopyingReferences();
Console.WriteLine();
Console.WriteLine();
ComparingReferences();
Console.WriteLine();
Console.WriteLine();
ComparingValues();
}
internal static void CopyingReferences()
{
var c1 = new Counter();
Counter c2 = c1;
Console.WriteLine($"c1: {c1.GetNextValue()}");
Console.WriteLine($"c1: {c1.GetNextValue()}");
Console.WriteLine($"c1: {c1.GetNextValue()}");
Console.WriteLine($"c2: {c2.GetNextValue()}");
Console.WriteLine($"c1: {c1.GetNextValue()}");
}
internal static void ComparingReferences()
{
var c1 = new Counter();
c1.GetNextValue();
Counter c2 = c1;
var c3 = new Counter();
c3.GetNextValue();
Console.WriteLine(c1.Count);
Console.WriteLine(c2.Count);
Console.WriteLine(c3.Count);
Console.WriteLine(c1 == c2);
Console.WriteLine(c1 == c3);
Console.WriteLine(c2 == c3);
Console.WriteLine(object.ReferenceEquals(c1, c2));
Console.WriteLine(object.ReferenceEquals(c1, c3));
Console.WriteLine(object.ReferenceEquals(c2, c3));
}
// With the default SDK settings, the code analysis rules detect the
// problem this example illustrates, reporting CA2013 warnings. This
// particular example makes this mistake on purpose, so we disable the
// warning.
#pragma warning disable CA2013
internal static void ComparingValues()
{
int c1 = new int();
c1++;
int c2 = c1;
int c3 = new int();
c3++;
Console.WriteLine(c1);
Console.WriteLine(c2);
Console.WriteLine(c3);
Console.WriteLine(c1 == c2);
Console.WriteLine(c1 == c3);
Console.WriteLine(c2 == c3);
Console.WriteLine(object.ReferenceEquals(c1, c2));
Console.WriteLine(object.ReferenceEquals(c1, c3));
Console.WriteLine(object.ReferenceEquals(c2, c3));
Console.WriteLine(object.ReferenceEquals(c1, c1));
}
}