
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is a static constructor in C#?
A static constructor is a constructor declared using a static modifier. It is the first block of code executed in a class. With that, a static constructor executes only once in the life cycle of class.
The following is an example of static constructors in C# −
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Difference { class Demo { static int val1; int val2; static Demo() { Console.WriteLine("This is Static Constructor"); val1 = 70; } public Demo(int val3) { Console.WriteLine("This is Instance Constructor"); val2 = val3; } private void show() { Console.WriteLine("First Value = " + val1); Console.WriteLine("Second Value = " + val2); } static void Main(string[] args) { Demo d1 = new Demo(110); Demo d2 = new Demo(200); d1.show(); d2.show(); Console.ReadKey(); } } }
- Related Articles
- How to call a static constructor or when static constructor is called in C#?
- Difference between Static Constructor and Instance Constructor in C#
- What is a copy constructor in C#?
- Can we define a static constructor in Java?
- Is it possible to create static constructor in java?
- What are the differences between a static block and a constructor in Java?
- What is a parameterized constructor in C# programs?
- What is a static class in C#?
- What is a static polymorphism in C#?
- What is converting constructor in C++ ?
- Is there any alternative solution for static constructor in java?
- What is a non-static class in C#?
- What is the order of execution of non-static blocks with respect to a constructor in Java?
- What is the default constructor in C#?
- What is default constructor in C# programs?

Advertisements