Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of C # static constructor

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the function of C# static constructor". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's go deep together to study and learn "what is the role of C# static constructor"?

C # constructor is a special member function, which is mainly used to allocate storage space for objects and initialize data members.

(1) the name of the C# constructor must have the same name as the class

(2) the C# constructor has no return type. It can take or not take parameters.

(3) the main function of the C # constructor is to initialize the class.

(4) when creating a new object of a class (using the new keyword), the system will automatically call the constructor of the class to initialize the new object.

The class of C # has two kinds of constructors: instance constructor and static constructor

Instance constructor of C # constructor:

It is responsible for initializing the instance variable in the class, which is called only when the user allocates memory for the object with the new keyword, and as a class of reference type, the instantiated object must be allocated on the managed heap (Managed Heap). The instance constructor is divided into default constructor and non-default constructor. Note that once the class has its own constructor, with or without parameters, the default constructor will be invalid, and only name a class without instantiating it. The constructor will not be called.

Static constructor for C # constructor:

(1) used to initialize static fields, read-only fields, etc.

(2) add the static keyword, but not the access modifier, because static constructors are private.

(3) the static constructor of a class is executed at most once in a given application domain. It is fired only when an instance of the class is created or any static member of the class is referenced, without arguments.

(4) static constructors are not inheritable and cannot be called directly.

(5) if the class contains a Main method to start execution, the static constructor of the class will be executed before the Main method is called. For any static field with initializers, those initializers must be executed in textual order when executing the static constructor of the class.

(6) if the static constructor is not written, and the class contains static fields with initial values, the compiler will automatically generate the default static constructor.

A class can have both an instance constructor and a static constructor, which is the only case where methods of the same name with the same parameter list can coexist.

Thank you for your reading, the above is the content of "what is the role of C# static constructor". After the study of this article, I believe you have a deeper understanding of the role of C# static constructor. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report