Classes and Objects
Fields
Fields store object state that methods and other code can read or update.
field
A field is a variable stored inside an object.
Fields
Fields.cs
Replay: real traced execution (multi-file project)
using System;
class Counter
{
public int Value;
}
class Program
{
static void Main()
{
int start = 2;
Counter counter = new Counter();
counter.Value = start;
counter.Value = counter.Value + 1;
Console.WriteLine($"start={start}");
Console.WriteLine($"value={counter.Value}");
}
}
using System;
class Counter
{
public int Value;
}
class Program
{
static void Main()
{
int start = 0;
Counter counter = new Counter();
counter.Value = start;
counter.Value = counter.Value + 1;
Console.WriteLine($"start={start}");
Console.WriteLine($"value={counter.Value}");
}
}
using System;
class Counter
{
public int Value;
}
class Program
{
static void Main()
{
int start = 5;
Counter counter = new Counter();
counter.Value = start;
counter.Value = counter.Value + 1;
Console.WriteLine($"start={start}");
Console.WriteLine($"value={counter.Value}");
}
}
start ← 2, counter ← Counter, counter.Value ← 2
9{10 static void Main()11 {12 int start→ 2 = 2; //@start=0, 513 Counter counter→ Counter = new Counter();14 counter.Value→ 2 = start2;15 counter.Value→ 3 = counter.Value + 1;1617 Console.WriteLine($"start={start2}");18 Console.WriteLine($"value={counter.Value3}");19 }outputstart=2 value=3
start ← 0, counter ← Counter, counter.Value ← 0
9{10 static void Main()11 {12 int start→ 0 = 0;13 Counter counter→ Counter = new Counter();14 counter.Value→ 0 = start0;15 counter.Value→ 1 = counter.Value + 1;1617 Console.WriteLine($"start={start0}");18 Console.WriteLine($"value={counter.Value1}");19 }outputstart=0 value=1
start ← 5, counter ← Counter, counter.Value ← 5
9{10 static void Main()11 {12 int start→ 5 = 5;13 Counter counter→ Counter = new Counter();14 counter.Value→ 5 = start5;15 counter.Value→ 6 = counter.Value + 1;1617 Console.WriteLine($"start={start5}");18 Console.WriteLine($"value={counter.Value6}");19 }outputstart=5 value=6