In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what are the differences between static variables and non-static variables in C#". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the difference between static variables and non-static variables in C#.
Static variables: static variables are declared using the static modifier to declare that the same static variable created to access all instances of the class through the class is the same value non-static variable without the static modifier declaration is called non-static variable when the class is instantiated to create the same non-static variable to access different instances of the same class through the object can be different values.
For automatic variables, it belongs to dynamic storage. But you can also use static to define it as a static automatic variable, or static local variable, thus becoming a static storage mode. From this point of view, a variable can be restated by static and changed the way it is stored.
The differences between static and non-static variables are as follows:
1. Memory allocation
Static variables exist in memory when the application is initialized and do not die until the end of the program of the class in which it resides, while non-static variables need to be instantiated before memory is allocated.
two。 Life cycle
The life cycle of static variables is the existence cycle of the application; the existence cycle of non-static variables depends on the existence cycle of the instantiated class.
3. Calling mode
Static variables can only be called through class. the static variable name is called, and the instance of the class cannot be called; the non-static variable can be accessed directly through the instantiated class name when the variable is instantiated.
4. Sharing mode
Static variables are global variables, which are shared by instance objects of all classes, that is, one instance changes the value of the static variable, and other similar instances read the changed value.
Non-static variables are local and are not shared.
5. Access mode
Static members cannot access non-static members; non-static members can access static members.
The static variable allocates memory when the class is loaded, and all objects created later use that memory, and the corresponding operation is to operate on this memory. It can also be thought of as an alternative global variable.
In WebSerivice, you want to initialize only once, instead of initializing every time, which takes up a lot of resources. You can also manage the invoked services, for example, if you want to queue up the services invoked each time, you can put the response information into Arraylist and queue up for execution in the static clock.
C # static constructor and static variable
Static constructor:
(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.
(6) for any static field with initializers, those initializers must be executed in textual order when executing the static constructor of the class.
(7) 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.
Classic examples:
/ * * 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 the static constructor of B-> {Console.WriteLine ("X = {0}, Y = {1}", A.X, B.Y); / / ⑤ output result Console.ReadLine ();}}
Some notes on the static constructor of C#:
Static constructor is a new feature of C #, but it seems to be rarely used. But we need to use it when we want to initialize some static variables. This constructor belongs to the class, not to any instance, which means that the constructor will only be executed once. That is, it is called automatically by .NET before creating * * instances or referencing any static members.
Class SimpleClass {/ / Static constructor static SimpleClass () {/ /}}
There are a few points to pay attention to when using static constructors:
1. The static constructor has neither access modifiers nor arguments. Because it is called by. Net, modifiers like public and private are meaningless.
2. When creating * class instances or any static members are referenced, .NET will automatically call the static constructor to initialize the class, that is, we cannot call the static constructor directly. There is no way to control when the static constructor is executed.
3. A class can have only one static constructor.
4. Constructors with no parameters can coexist with static constructors. Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.
5. Run only once at most.
6. Static constructors cannot be inherited.
7. If the static constructor is not written and the class contains static members with initializations, the compiler automatically generates the default static constructor.
These are all the contents of the article "what are the differences between static variables and non-static variables in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.