What is a Static Constructor in C#?

πŸ’‘ Concept: Static Constructor

A static constructor in C# is used to initialize static fields of a class or to perform actions that only need to be done once, regardless of the number of class instances.

πŸ“˜ Quick Intro

Static constructors are special methods without parameters that initialize static data members. They are called automatically before the first instance is created or any static member is accessed.

🧠 Analogy

Think of a static constructor like setting up the stage before the first act in a play. It prepares everything once, before any actors (objects) come on stage.

πŸ”§ Technical Explanation

  • πŸ”Ή Static constructors do not take parameters and cannot be overloaded.
  • πŸ”Ή They are called automatically before any static members are accessed.
  • πŸ”Ή Only one static constructor per class is allowed.
  • πŸ”Ή You cannot call a static constructor explicitly.
  • πŸ”Ή Useful for initializing static variables and performing one-time operations.

🎯 Use Cases

  • ⚑ Initializing static variables that require logic.
  • πŸ” Setting up logging or configuration settings.
  • πŸ›  Preparing resources or file paths before usage.
  • πŸ“¦ Enforcing one-time setup for Singleton patterns.

πŸ’» Code Example

public class Config
{
    public static string AppName;

    // Static Constructor
    static Config()
    {
        Console.WriteLine("Static constructor called.");
        AppName = "FullStackPrep.Dev";
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Config.AppName); // Triggers static constructor
    }
}

❓ Interview Q&A

Q1: What is a static constructor?
A: A constructor that initializes static data and runs once for the type.

Q2: When is a static constructor called?
A: Automatically before any static members are accessed.

Q3: Can a static constructor take parameters?
A: No, they must be parameterless.

Q4: Can you overload a static constructor?
A: No, only one is allowed per class.

Q5: Is it mandatory to define a static constructor?
A: No, it’s optional and used when needed.

Q6: Can you call a static constructor directly?
A: No, it’s invoked automatically.

Q7: Can a static constructor access instance members?
A: No, only static members can be accessed.

Q8: How often is a static constructor called?
A: Exactly once for the type.

Q9: Is thread safety guaranteed in static constructors?
A: Yes, the runtime ensures it runs only once.

Q10: Can static constructors throw exceptions?
A: Yes, but the type becomes unusable after that.

πŸ“ MCQs

Q1. When is a static constructor called?

  • When the program starts
  • Before instance is created
  • Before any static member is accessed
  • After all instances

Q2. How many static constructors can a class have?

  • Multiple
  • None
  • Only one
  • Depends on overloads

Q3. Can static constructors be overloaded?

  • Yes
  • No
  • Only if private
  • Depends on parameters

Q4. Can you pass arguments to a static constructor?

  • Yes
  • No
  • Optional only
  • Through config file

Q5. What is true about static constructors?

  • Called manually
  • Only for inheritance
  • They are invoked automatically
  • Cannot use them in interfaces

Q6. Can static constructors be inherited?

  • Yes
  • No
  • Only with virtual keyword
  • Only in sealed classes

Q7. Can static constructors initialize static fields?

  • No
  • Only instance
  • Yes
  • Sometimes

Q8. Do static constructors have return types?

  • Yes
  • No
  • Void
  • Static

Q9. Are static constructors thread-safe?

  • No
  • Only in .NET 5+
  • Yes, by CLR
  • Only when locked

Q10. Can a class have both static and instance constructors?

  • No
  • Yes
  • Only if sealed
  • Only with overloads

πŸ’‘ Bonus Insight

Use static constructors wisely to ensure one-time setup of static members. Avoid logic that can failβ€”any unhandled exception prevents the class from being used.

πŸ“„ PDF Download

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

πŸ” Navigation

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

Tags: