- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to call a static constructor or when static constructor is called in C#?
Static constructor are called automatically before the first instance is created or any static members are referenced.
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only.
In c#, only one static constructor is allowed to create
Static constructors have the following properties −
A static constructor does not take access modifiers or have parameters.
A class or struct can only have one static constructor.
Static constructors cannot be inherited or overloaded.
A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically.
The user has no control on when the static constructor is executed in the program.
Example
class Program{ static Program(){ // Your Code } static void Main(){ Console.ReadLine(); } }
- Related Articles
- What is a static constructor in C#?
- Difference between Static Constructor and Instance Constructor in C#
- When is copy constructor called in C++?
- Is it possible to create static constructor in java?
- Can we define a static constructor in Java?
- Is there any alternative solution for static constructor in java?
- How to call the constructor of a superclass from a constructor in java?
- Can we initialize static variables in a default constructor in Java?
- Do static variables get initialized in a default constructor in java?
- How to call Private Constructor in Java
- What are the differences between a static block and a constructor in Java?
- Order of Constructor/ Destructor Call in C++
- Sequence of execution of, instance method, static block and constructor in java?
- What is the order of execution of non-static blocks with respect to a constructor in Java?
- Why Java wouldn't allow initialization of static final variable in a constructor?
