What are Static Methods in C#?

πŸ’‘ Concept: Static Methods

A static method belongs to the class rather than any object of the class. It can be called without creating an instance of the class and only accesses static data.

πŸ“˜ Quick Intro

Static methods are declared using the static keyword and are commonly used for utility or helper operations. They can't access instance members directly.

🧠 Analogy

Imagine a vending machine: pressing a button (static method) gives you a snack without needing to personalize or remember who you are (no instance needed). Each person gets the same result from the same action.

πŸ”§ Technical Explanation

  • Declared using the static keyword.
  • Cannot access non-static (instance) fields or methods.
  • Can be called using the class name: ClassName.Method().
  • Commonly used for utility, factory, or configuration methods.
  • Memory allocated once per application domain.

🎯 Use Cases

  • Utility/helper methods (e.g., string formatting, math calculations).
  • Factory methods returning objects.
  • Global configuration readers/writers.
  • Extension methods (must be static).

πŸ’» Code Example

public class MathHelper
{
    public static int Square(int num)
    {
        return num * num;
    }
}

// Usage
int result = MathHelper.Square(5); // 25

❓ Interview Q&A

Q1: What is a static method?
A: A method that belongs to the class, not instances.

Q2: How do you call a static method?
A: Using the class name directly: ClassName.Method().

Q3: Can static methods access instance variables?
A: No, only static members.

Q4: Can a static method call another static method?
A: Yes, it can.

Q5: What is the memory implication of static methods?
A: They’re loaded once per AppDomain.

Q6: Where are static methods commonly used?
A: Utility classes, configuration readers, factory patterns.

Q7: Can static methods be virtual or overridden?
A: No, static methods can't be overridden.

Q8: Are static methods thread-safe?
A: Not inherently; you must manage synchronization.

Q9: What is a static class?
A: A class that only contains static members and cannot be instantiated.

Q10: Can extension methods be non-static?
A: No, they must be declared static in a static class.

πŸ“ MCQs

Q1. What keyword defines a static method?

  • abstract
  • static
  • override
  • sealed

Q2. What is a static method tied to?

  • The instance
  • The class
  • The module
  • The file

Q3. Can a static method access instance fields?

  • Yes
  • No
  • Only with ref
  • Only through events

Q4. What is a common use case for static methods?

  • Database connections
  • Memory allocation
  • User interfaces
  • Helper or utility methods

Q5. Which of these is true about static methods?

  • They use new()
  • They override virtual
  • They are called using class name
  • They must return string

Q6. Can static methods be inherited?

  • Yes
  • No
  • Only in base class
  • Only in interface

Q7. Can a static method return an object?

  • No
  • Only value types
  • Yes
  • Only static objects

Q8. What is the limitation of static methods?

  • No memory usage
  • Too slow
  • Cannot access instance members
  • Only one return value

Q9. Are static methods thread-safe by default?

  • Yes
  • No
  • Only in Windows
  • Only with lock

Q10. Where are static methods stored?

  • Per instance
  • In database
  • In application memory (AppDomain)
  • In config file

πŸ’‘ Bonus Insight

Static methods provide performance and simplicity when no state needs to be preserved. Overusing them may lead to tightly coupled code and testing challenges. Use them wisely for stateless, repeatable logic.

πŸ“„ PDF Download

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

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: