Static vs Non-Static Constructor in C#
Sharad Aade

Sharad Aade @sharadaade

About: Content writer @DryRun

Location:
India
Joined:
Feb 8, 2024

Static vs Non-Static Constructor in C#

Publish Date: Jul 16
5 0

NOTE:- Constructors do not have a return type, even void.

Why do we need a constructor?
Ans: - To initialise the memory of a variable or field.

  • Every non-static class contains an implicit constructor if not defined explicitly.

  • If we define an explicit constructor with static modifiers, it is referred to as a static constructor, whereas others are non-static constructors.

  • Non-static constructors may be implicitly or explicitly provided.

  • If you do not provide an explicit one, then the compiler provides an implicit constructor.

  • Static field/variable initialised by static constructor.

  • Static constructors are implicitly called, but non-static constructors are explicitly called.

  • Static constructor executes immediately once the execution of the class starts.

  • Static constructor executes only once, but non-static constructor executes zero times if no instance is created and n times if n instance is created.

  • Non-static constructors may be parameterised, but static constructors are parameterless because they are implicitly called and it is the first block of code run under the class.

  • Non-static constructors may be overloaded, but a static constructor can not be overloaded.

✍️Thank you!✍️

Comments 0 total

    Add comment