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

How to use C # static functions and variables

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to use C# static functions and variables". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Before learning the static method of C #, let's first take a look at what are the static functions and variables of C # and what are the specific forms and precautions of the static functions and variables of C #? So let's take a look at a classic example and code:

(1) used to initialize static fields, read-only fields, and so on.

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

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

(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 are 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.

The following is further illustrated with example code:

/ * static construction function practice * (1) ①②③. Output the result for the execution order * (2): static A () * static B () * X = 1, Y = 2 * * / using System; class A {public static int X; static A () / / ④ returns to ③ {X = B.Y + 1; / / Console.WriteLine ("static A ()");} / /} class B {public static int Y = A.X + 1 / / ③ calls the static member of A, / / go to the static constructor of A-> static B () / / ② if the static field with initializer, / / execute the static constructor of this class, / / first execute those initializers in textual order. / / go to the initializer-> {Console.WriteLine ("static B ()");} static void Main () / / ① program entry, / / 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. / / go to B's static constructor-> {/ / Console.WriteLine ("X = {0}, Y = {1}", A.X, B.Y); / / ⑤ output result Console.ReadLine ();}} "how to use C# static functions and variables" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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