Static members belong to the class itself, while instance members belong to each object made from the class. This helps organize data and actions that are shared or unique.
Static members vs instance members in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
class ClassName { static int staticMember; // shared by all int instanceMember; // unique to each object static void StaticMethod() { } void InstanceMethod() { } }
Static members use the static keyword.
Instance members do not use static and require an object to access.
Examples
C Sharp (C#)
class Car { public static int numberOfCars = 0; // shared count public string color; // unique color public Car(string c) { color = c; numberOfCars++; } }
C Sharp (C#)
Car.numberOfCars = 5; // Access static member without object Car myCar = new Car("red"); string carColor = myCar.color; // Access instance member via object
Sample Program
This program shows how static and instance members work. The static dogCount counts all dogs made. Each dog has its own name and can bark.
C Sharp (C#)
using System; class Dog { public static int dogCount = 0; public string name; public Dog(string dogName) { name = dogName; dogCount++; } public static void ShowDogCount() { Console.WriteLine($"Total dogs: {dogCount}"); } public void Bark() { Console.WriteLine($"{name} says: Woof!"); } } class Program { static void Main() { Dog.ShowDogCount(); Dog dog1 = new Dog("Buddy"); Dog dog2 = new Dog("Max"); Dog.ShowDogCount(); dog1.Bark(); dog2.Bark(); } }
Important Notes
Static members exist once per class, not per object.
Instance members need an object to be used.
Use static for shared data or helpers, instance for unique object data.
Summary
Static members belong to the class and are shared by all objects.
Instance members belong to each object and hold unique data.
Access static members with the class name; access instance members with an object.
Practice
1. Which statement correctly describes a static member in C#?
easy
Solution
Step 1: Understand static member concept
Static members belong to the class itself, not to any individual object.Step 2: Compare with instance members
Instance members belong to objects and hold unique data per object, unlike static members.Final Answer:
It belongs to the class and is shared by all instances. -> Option AQuick Check:
Static member = shared by class [OK]
Hint: Static = shared by class, instance = unique per object [OK]
Common Mistakes:
- Confusing static with instance members
- Thinking static members are unique per object
- Believing static members require object to access
2. Which of the following is the correct way to declare a static field in a C# class?
easy
Solution
Step 1: Recall C# static field syntax
The correct order is access modifier, then 'static', then type and name.Step 2: Check each option
public static int count; matches the correct syntax: 'public static int count;'. Others have wrong order or missing keywords.Final Answer:
public static int count; -> Option AQuick Check:
Access modifier + static + type + name [OK]
Hint: Use 'public static' before type for static fields [OK]
Common Mistakes:
- Placing 'static' after the type
- Omitting 'static' keyword
- Incorrect order of keywords
3. What will be the output of the following C# code?
class Counter {
public static int count = 0;
public Counter() {
count++;
}
}
class Program {
static void Main() {
Counter c1 = new Counter();
Counter c2 = new Counter();
Console.WriteLine(Counter.count);
}
}medium
Solution
Step 1: Understand static field behavior
The static field 'count' is shared by all instances and starts at 0.Step 2: Trace constructor calls
Each new Counter() increments 'count' by 1. Two objects created, so count becomes 2.Final Answer:
2 -> Option BQuick Check:
Static count increments twice = 2 [OK]
Hint: Static fields keep shared state across all objects [OK]
Common Mistakes:
- Thinking count resets per object
- Confusing instance and static fields
- Expecting compilation error due to static access
4. Identify the error in this C# code snippet:
class Example {
public static int value = 10;
public int GetValue() {
return value;
}
}medium
Solution
Step 1: Check static field access rules
Instance methods can access static fields directly without error.Step 2: Verify code correctness
Method GetValue returns static field 'value' correctly; no syntax or access errors.Final Answer:
No error; code compiles and runs correctly. -> Option DQuick Check:
Instance method can access static field [OK]
Hint: Instance methods can access static fields directly [OK]
Common Mistakes:
- Thinking instance methods cannot access static fields
- Believing GetValue must be static
- Assuming static fields must be private
5. You want to count how many objects of a class are created, but also keep a unique ID for each object starting from 1. Which approach correctly uses static and instance members?
hard
Solution
Step 1: Understand counting objects with static field
A static counter shared by all objects can track total created objects.Step 2: Assign unique ID per object using instance field
Each object gets its own instance ID assigned from the static counter value.Final Answer:
Use a static counter incremented in constructor; assign its value to an instance ID field. -> Option CQuick Check:
Static counter + instance ID = unique IDs [OK]
Hint: Static counts total; instance stores unique ID per object [OK]
Common Mistakes:
- Using instance counter for total count
- Assigning static ID per object (not unique)
- Not incrementing counter in constructor
