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!