

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Static Constructor and Instance Constructor in C#
Static Constructor
A static constructor is a constructor declared using 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.
Instance Constructor
Instance constructor initializes instance data. Instance constructor is called when an object of class is created.
The following example shows the difference between static and instance constructor 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(); } } }
Output
This is Static Constructor This is Instance Constructor This is Instance Constructor First Value = 70 Second Value = 110 First Value = 70 Second Value = 200
- Related Questions & Answers
- Difference Between Constructor and Destructor
- Sequence of execution of, instance method, static block and constructor in java?
- Difference between constructor and method in Java
- How to call a static constructor or when static constructor is called in C#?
- Difference Between Constructor Injection and Setter Injection in Spring
- Difference Between Copy Constructor and Assignment Operator in C++
- What are the differences between default constructor and parameterized constructor in Java?
- What is a static constructor in C#?
- What are the differences between a static block and a constructor in Java?
- Can we define a static constructor in Java?
- What is the difference between Static class and Singleton instance in C#?
- What is the difference between new operator and object() constructor in JavaScript?
- What is the difference between getter/setter methods and constructor in Java?
- Class with a constructor to initialize instance variables in Java
- Difference Between Schema and Instance
Advertisements