[Go] static statics variable in C #

Source: Internet
Author: User

Logitechyan static variables in C #

C # Static variables are declared with the static modifier, created when the class is instantiated, accessing variables declared with the static modifier without a static variable called nonstatic variables, created when an object is instantiated, access to all instances of a class through an object, the same C # static variable is the same value, The same non-static variable of a different instance of the same class can be a different value. Static functions cannot be implemented with non-static members, such as non-static variables, non-static functions, and so on. Use the static modifier to declare static members that belong to the type itself rather than to a particular object can be used for classes, fields, methods, properties, operators, events, and constructors, but not for types other than indexers, destructors, or classes

  

Static global variables

Before a global variable, the variable is defined as a static global variable, plus the keyword static.

features :

This variable allocates memory in the global data area

Initialize: If not explicitly initialized, it is implicitly initialized to 0.

Static local Variables

definition :

Static local variables are defined when the static keyword is preceded by a local variable.

features :

This variable allocates memory in the global data area.

Initialize: If not explicitly initialized, it is implicitly initialized to 0.

It always resides in the global data area until the program finishes running. But its scope is a local scope, and when the function or block of statements that defines it ends, its scope ends.

Static data members
features :

Memory allocation:

The global data area of the program is assigned.

Initialize and define:    

A, static data members are defined to allocate space, so they cannot be defined in a class declaration.

B, in order to avoid multiple use of the class in the source file, the duplicate definition, where it cannot be defined in the header file of the class.

c, static data members are required to be present at the beginning of a program, so the best place to initialize it is implemented inside the class.

Characteristics  

A, the effect on the Public,protected,private keyword it is like a regular data member,

b, because its space is allocated in the global data area and belongs to all objects of this class, it does not belong to a particular class object, and its scope is visible when no class object is produced, that is, we can manipulate it without producing an instance of the class.

Access Form  

Class object name. static data member name

A static data member that is primarily used on properties owned by all instances of the class. For example, for a deposit class, the account number is different relative to each instance, but the interest per instance is the same. Therefore, interest should be set as a static data member of the deposit class. There are two benefits, first, regardless of how many deposit class objects are defined, the interest data members share the memory allocated in the global zone, thus saving storage space. Second, once the interest needs to be changed, the interest in all the deposit classes changes all the time, because they are actually sharing one thing.

  

Static member functions
features :

A static member function is associated with a class and is not associated with the object of the class.

Static member functions cannot access non-static data members. The reason is simple: non-static data members belong to a particular class instance.
function :

Primarily used for operations on static data members.

invocation Form :

Class object name. static member function name ()

  

An example and analysis of static statically variable

Example :

Using System; Namespace Teststatic {    class Class1    {        static int i = Getnum ();        Int J = Getnum ();          static int num = 1;                 static int Getnum ()        {            return num;        }          static void Main (string[] args)        {            Console.WriteLine ("I={0}", i);            Console.WriteLine ("J={0}", new Class1 (). j);            Console.read ();}}}    

now analyze the above code :

Console.WriteLine (String. Format ("i={0}", i)); Here I is the static variable, and the class Class1 is the first to be referenced, first to allocate memory for all the static variables inside the Class1. Although there are hyper-threading techniques now, the instructions are executed sequentially in the logic or one line, so the memory is first allocated for the static int I, and the default value of int in that memory is 0, then the static int num variable is allocated memory, and the value is of course 0.

Then take the second step, assign a value to the variable: first assign the static int i variable, i=getnum (), see the code inside the Getnum, that is, return num, this time num value is 0, so I is 0. The variable num is then assigned a value, num=1, and Num is 1 after this line of code executes.

So the final result is:

I=0 j=1

[Go] static statics variable in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.