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

Learn More About C# πŸ“š

1. What is C#? πŸ‘‰ Explained
2. Main Features of C# πŸ‘‰ Explained
3. Difference Between C# and Java πŸ‘‰ Explained
4. Common Language Runtime (CLR) in C# πŸ‘‰ Explained
5. Common Type System (CTS) in C# πŸ‘‰ Explained
6. Common Language Specification (CLS) in C# πŸ‘‰ Explained
7. Value Types vs Reference Types in C# πŸ‘‰ Explained
8. What is a Namespace in C#? πŸ‘‰ Explained
9. Purpose of the 'using' Keyword in C# πŸ‘‰ Explained
10. Different Data Types in C# πŸ‘‰ Explained
11. Difference Between int and Int32 in C# πŸ‘‰ Explained
12. Difference Between float, double, and decimal in C# πŸ‘‰ Explained
13. What is the Default Value of a Boolean in C#? πŸ‘‰ Explained
14. What is Boxing and Unboxing in C# πŸ‘‰ Explained
15. What are the Different Types of Operators in C# πŸ‘‰ Explained
16. Difference Between Equals and == in C# πŸ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# πŸ‘‰ Explained
18. What is the Ternary Operator in C# πŸ‘‰ Explained
19. How Does the Switch Statement Work in C# πŸ‘‰ Explained
20. What is Object-Oriented Programming in C# πŸ‘‰ Explained
21. What are the Four Pillars of OOP in C# πŸ‘‰ Explained
22. What is Encapsulation in C# πŸ‘‰ Explained
23. What is Inheritance in C# πŸ‘‰ Explained
24. What is Polymorphism in C# πŸ‘‰ Explained
25. What is Abstraction in C# πŸ‘‰ Explained
26. What is an Abstract Class in C# πŸ‘‰ Explained
27. What is an Interface in C# πŸ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? πŸ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# πŸ‘‰ Explained
30. How Do You Create a Class in C#? πŸ‘‰ Explained
31. What is a Constructor in C# πŸ‘‰ Explained
32. What Are the Types of Constructors in C# πŸ‘‰ Explained
33. What is a Static Constructor in C# πŸ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# πŸ‘‰ Explained
35. What is the Use of 'this' Keyword in C# πŸ‘‰ Explained
36. What is a Destructor in C# πŸ‘‰ Explained
37. What is Object Initializer Syntax in C# πŸ‘‰ Explained
38. What is the Difference Between Field and Property in C# πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup