Answered by:
Does static field required static constructor?

Question
-
User-240513752 posted
Suppose I have a static field , in a normal class(not static) then the static field required the static constructor mandatory or it will not necessary for compilation.
Wednesday, March 15, 2017 8:08 PM
Answers
-
User-1509636757 posted
When static constructor is not defined in class, static fields are initialized just before their use (In fact, static constructors considered to be slow). Here are some points related:
- When static constructor is not defined in class, static fields are initialized just before their use.
- When static constructor is defined in class, static fields are initialized just before their use or as part of (before) instance creation.
- If no static field is accessed within a static method and that static method is called. the static fields will be initialized only if static constructor is defined in that class.
- If possible static constructor should be avoided in a class.
References:
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 15, 2017 8:24 PM
All replies
-
User-1509636757 posted
When static constructor is not defined in class, static fields are initialized just before their use (In fact, static constructors considered to be slow). Here are some points related:
- When static constructor is not defined in class, static fields are initialized just before their use.
- When static constructor is defined in class, static fields are initialized just before their use or as part of (before) instance creation.
- If no static field is accessed within a static method and that static method is called. the static fields will be initialized only if static constructor is defined in that class.
- If possible static constructor should be avoided in a class.
References:
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 15, 2017 8:24 PM -
User303363814 posted
Why do you ask? Create a class, add a static field. Does it compile?
Thursday, March 16, 2017 1:43 AM -
User-707554951 posted
Hi khan_1,
Static field does not required static constructor.
Besides, as far as I kknow. A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
https://msdn.microsoft.com/en-us/library/98f28cdx.aspx
http://www.c-sharpcorner.com/uploadfile/abhikumarvatsa/static-and-non-static-fields-in-C-Sharp/
Best regards
Cathy
Thursday, March 16, 2017 9:06 AM