Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Instance Constructor vs Static Constructor

What are the differences between the two? I've only used one kind of constructor and I believe it's the static constructor. Only familiar with C++ and Java.

like image 440
RoR Avatar asked Sep 30 '10 20:09

RoR


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


3 Answers

Static constructor is called the first time your class is referenced i.e.

MyClass.SomeStaticMethod()

Instance constructor is called every time you do 'MyClass dummy = new MyClass()' i.e. create instance of the class

Semantically first is used when you want to ensure that some static state is initialized before it is accessed, the other is used to initialize instance members.

like image 90
Muhammad Hasan Khan Avatar answered Sep 23 '22 08:09

Muhammad Hasan Khan


Static constructors allow you to initialize static variables in a class, or do other things needed to do in a class after it's first referenced in your code. They are called only once each time your program runs.

Static constructors are declared with this syntax, and can't be overloaded or have any parameters because they run when your class is referenced by its name:

static MyClass()
{
}

Instance constructors are the ones that are called whenever you create new objects (instances of classes). They're also the ones you normally use in Java and most other object-oriented languages.

You use these to give your new objects their initial state. These can be overloaded, and can take parameters:

public MyClass(int someNumber) : this(someNumber, 0) {}

public MyClass(int someNumber, int someOtherNumber)
{
    this.someNumber = someNumber;
    this.someOtherNumber = someOtherNumber;
}

Calling code:

MyClass myObject = new MyClass(100, 5);
like image 24
BoltClock Avatar answered Sep 24 '22 08:09

BoltClock


The static constructor runs only once for all instances or uses of the class. It will run the first time you use the class. Normal constructors run when you instantiate an object of the class.

Everything you should need to know about static constructors can be found here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors

like image 25
Steven Oxley Avatar answered Sep 22 '22 08:09

Steven Oxley