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

Static members vs instance members in C Sharp (C#) - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Static members vs instance members
Start
Create Class
Define Static Member
Define Instance Member
Create Object Instance
Access Instance Member via Object
Access Static Member via Class
Use Members
End
This flow shows how static members belong to the class itself and instance members belong to each object created from the class.
Execution Sample
C Sharp (C#)
class Car {
  public static int wheels = 4;
  public string color;

  public Car(string c) {
    color = c;
  }
}

Car car1 = new Car("red");
Console.WriteLine(Car.wheels);
Console.WriteLine(car1.color);
This code defines a class with a static member and an instance member, creates an object, and accesses both members.
Execution Table
StepActionEvaluationResult
1Define class Car with static wheels=4 and instance colorClass readyStatic wheels=4, no instances yet
2Create car1 = new Car("red")Constructor sets colorcar1.color = "red"
3Access Car.wheelsStatic member accessed via classOutput: 4
4Access car1.colorInstance member accessed via objectOutput: red
5End of executionAll members accessedProgram ends
💡 Program ends after accessing static and instance members
Variable Tracker
VariableStartAfter Step 2Final
Car.wheels444
car1.colorundefinedredred
Key Moments - 2 Insights
Why do we access 'wheels' using the class name but 'color' using the object name?
Because 'wheels' is static and belongs to the class itself, so we use the class name (see step 3). 'color' is an instance member, so it belongs to each object and we access it via the object (see step 4).
Can we access instance members without creating an object?
No, instance members need an object to exist. Without creating 'car1', 'color' has no value (see step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of Car.wheels at step 3?
Ared
Bundefined
C4
D0
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step is the instance member 'color' assigned a value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns for when car1.color is set.
If we try to access 'color' using the class name like Car.color, what would happen?
AIt will cause a compile-time error
BIt will output the color value
CIt will output null
DIt will output 4
💡 Hint
Instance members require an object; see key_moments about access rules.
Concept Snapshot
Static members belong to the class and are shared by all instances.
Instance members belong to each object separately.
Access static members via ClassName.member.
Access instance members via object.member.
Static members exist even if no objects are created.
Instance members require an object to exist.
Full Transcript
This example shows a class Car with a static member wheels and an instance member color. The static member wheels is shared by all cars and accessed using the class name. The instance member color is unique to each car object and accessed through the object. We create car1 with color 'red'. Accessing Car.wheels gives 4, and car1.color gives 'red'. Static members do not need an object, but instance members do. Trying to access instance members without an object causes errors.