Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static variable allocation time in .net

Tags:

c#

When static variable is allocated i.e. when you declare class or at the time of object creation?

like image 577
Kiran Thokal Avatar asked Mar 27 '26 22:03

Kiran Thokal


1 Answers

It is compiled into the static constructor. So the first time anyone creates an object of the class or calls a static method or property on it, the initialization occurs.

Edit: when it is important to you if initialization happens before of after your own static constructor code (and some other edge cases),check the link in the comment by divo.

like image 189
Teun D Avatar answered Apr 02 '26 22:04

Teun D