Difference Between Static and Non-Static Members in C#

๐Ÿ’ก Concept: Static vs Non-Static Members

Static members belong to the type itself, while non-static members (also called instance members) belong to an instance of the class. The distinction affects memory, scope, and usage in C# applications.

๐Ÿ“˜ Quick Intro

Static members are shared across all instances and accessed using the class name. Non-static members are specific to an object instance and require instantiation to access. Choosing between them depends on whether data and behavior should be shared or isolated.

๐Ÿง  Analogy

Imagine a university: the university name is the same for all students โ€” this is a static member. But each student has their own ID and grades โ€” those are non-static members. The shared identity vs personal details concept aligns well with static vs instance data.

๐Ÿ”ง Technical Explanation

  • โš™๏ธ Static members belong to the class, not individual objects.
  • ๐Ÿง Non-static members are tied to instances and require object creation.
  • ๐Ÿ“Œ Static methods can't access instance members directly.
  • ๐Ÿš€ Static data is initialized once and remains until program ends.
  • ๐Ÿ”’ Use static members for shared logic or constants across instances.

๐ŸŽฏ Use Cases

  • โœ… Static: Utility/helper classes (e.g., Math), caching, logging services.
  • โœ… Non-static: Business models, entity classes (e.g., Customer, Order).
  • โœ… Static is great for Singleton patterns or shared counters.
  • โœ… Use non-static when each object must maintain unique data.

๐Ÿ’ป Code Example

public class Counter
{
    public static int GlobalCount = 0; // Static member
    public int InstanceCount = 0;      // Non-static member

    public Counter()
    {
        GlobalCount++;
        InstanceCount++;
    }
}

class Program
{
    static void Main()
    {
        Counter c1 = new Counter();
        Counter c2 = new Counter();

        Console.WriteLine(Counter.GlobalCount); // 2
        Console.WriteLine(c1.InstanceCount);    // 1
        Console.WriteLine(c2.InstanceCount);    // 1
    }
}

โ“ Interview Q&A

Q1: What is the key difference between static and non-static members?
A: Static belongs to the class; non-static belongs to the instance.

Q2: How do you access static members?
A: Using the class name directly, e.g., ClassName.Member.

Q3: Can a static method access instance variables?
A: No, it cannot access instance members directly.

Q4: What is shared among all instances in a class?
A: Static members.

Q5: Are static variables thread-safe by default?
A: No, you must handle synchronization.

Q6: Can constructors be static?
A: Yes, static constructors exist to initialize static fields.

Q7: Can non-static members call static ones?
A: Yes, from instance context.

Q8: Where are static members stored in memory?
A: In the application domain memory, not in object heap.

Q9: Can static classes have instance methods?
A: No, they can only have static members.

Q10: Can static methods be overridden?
A: No, they cannot participate in polymorphism.

๐Ÿ“ MCQs

Q1. What is true about static members?

  • They are per object
  • They require instance
  • They belong to the class
  • They run asynchronously

Q2. How are non-static members accessed?

  • Through class name
  • Only in static classes
  • Through object instance
  • They are private

Q3. What is a static method?

  • Can be inherited
  • Always virtual
  • Belongs to instance
  • Belongs to class

Q4. Which member is NOT shared among instances?

  • Static field
  • Instance field
  • Static method
  • Static property

Q5. What is the default scope of static variables?

  • Global application
  • Object-level
  • Thread-level
  • Global within the class

Q6. Can instance methods call static members?

  • No
  • Yes
  • Only if sealed
  • Only via reflection

Q7. Where are static members stored?

  • Stack
  • Heap
  • In type-level memory
  • In thread context

Q8. Are static members copied per object?

  • Yes
  • No
  • Optional
  • Depends on constructor

Q9. Which method can access instance data?

  • Static
  • Instance
  • Global
  • Extension only

Q10. Which is better for shared utility logic?

  • Instance method
  • Property
  • Static method
  • Constructor

๐Ÿ’ก Bonus Insight

Static members improve performance and reduce memory usage when used correctly. However, misuse (especially in multi-threaded apps) can introduce bugs or state leakage โ€” use with care.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: