0
0
C Sharp (C#)programming~10 mins

Struct declaration and behavior in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Struct declaration and behavior
Declare struct type
Create struct variable
Assign values to fields
Use struct variable
Copy struct variable?
Yes
Create independent copy
Modify copy does NOT affect original
End
This flow shows how a struct is declared, used, and how copying creates independent values.
Execution Sample
C Sharp (C#)
struct Point {
  public int X;
  public int Y;
}

Point p1 = new Point { X = 1, Y = 2 };
Point p2 = p1;
p2.X = 5;
This code declares a struct Point, creates one instance, copies it, and changes the copy's field.
Execution Table
StepActionVariable StatesNotes
1Declare struct Point with fields X and YNo variables yetStruct type created
2Create p1 with X=1, Y=2p1: {X=1, Y=2}p1 holds values 1 and 2
3Copy p1 to p2p1: {X=1, Y=2}, p2: {X=1, Y=2}p2 is a separate copy of p1
4Change p2.X to 5p1: {X=1, Y=2}, p2: {X=5, Y=2}p2 changed, p1 unchanged
5Endp1: {X=1, Y=2}, p2: {X=5, Y=2}Struct copy is independent
💡 Execution ends after modifying p2; p1 remains unchanged because structs are value types.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
p1undefined{X=1, Y=2}{X=1, Y=2}{X=1, Y=2}{X=1, Y=2}
p2undefinedundefined{X=1, Y=2}{X=5, Y=2}{X=5, Y=2}
Key Moments - 3 Insights
Why does changing p2.X not affect p1.X?
Because structs are value types, copying p1 to p2 creates a new independent copy. See execution_table step 4 where p2.X changes but p1.X stays the same.
Is the struct variable a reference or a value?
Struct variables hold values directly, not references. This is why copying creates independent copies, as shown in execution_table step 3.
What happens if we assign one struct variable to another?
A full copy of the struct's data is made. Both variables then hold separate copies, so changes to one do not affect the other (execution_table step 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of p1.X?
A0
B5
C1
DUndefined
💡 Hint
Check the Variable States column at step 4 in execution_table.
At which step does p2 get its own copy of p1's data?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look for the step where p2 is assigned values from p1 in execution_table.
If structs were reference types, what would happen when p2.X changes?
Ap1.X would stay the same
Bp1.X would also change
Cp2.X would revert to p1.X
DCompilation error
💡 Hint
Think about how reference types share the same data, unlike value types shown in variable_tracker.
Concept Snapshot
struct StructName {
  public Type Field1;
  public Type Field2;
}

- Structs are value types.
- Assigning copies all data.
- Changes to copies do not affect originals.
Full Transcript
This visual trace shows how a struct is declared and used in C#. First, the struct type Point is declared with two integer fields X and Y. Then, a variable p1 of type Point is created and initialized with X=1 and Y=2. Next, p1 is copied to p2, creating an independent copy. When p2.X is changed to 5, p1.X remains 1, showing that structs hold values directly and copying creates separate instances. This behavior is different from reference types where variables share the same data. The key moments highlight why changes to copies do not affect originals and clarify that structs are value types. The quiz questions reinforce understanding of when copies happen and how values change during execution.