A summary of learning about static members and methods in C #

Source: Internet
Author: User

A summary of learning about static members and methods in C #

Data members:
Data members can be divided into static variables, instance variables, and two.
Static members: Static member variables are associated with a class and can be used as a "common" variable in a class (a commonality of expression), and he does not rely on the existence of a particular object, accessed by the name of the multibyte variable when it accesses the class name.

Instance member: an instance member variable is associated with an object, and accessing an instance member variable depends on the existence of the instance.

Function Members:
Methods can be mainly divided into static methods, example methods

Static methods: Static methods are methods that are not part of a particular object, static methods can access static member variables, static methods can not directly access instance variables, and in the case of instance function calls, instance variables are passed as arguments to static methods. Static
Method also cannot invoke an instance method directly, it can be called indirectly, first to create an instance of a class, and then to invoke a static method through this particular object.

Instance method: The execution of an instance method is associated with a particular object, and his execution requires an object to exist. Instance methods can directly access static variables and instance variables, and instance methods can directly access instance methods, static methods, and static methods.
The multibyte variable name is the way to add a dot to the class name. When multiple instance objects exist, the memory is not a copy of a particular instance method, but all objects of the same class share a copy of each instance method (the instance method occupies only a "set of
"space).

If a member in a class is declared as static, the member is said to be a statically member. In general, static members are owned by the class, and non-static members belong to instances of the class. Each instance of a class is created in memory for non-static members
Area. The static members of the class are all classes, shared for all instances of the class. No matter how many copies this class creates, a static member occupies only one area of memory.

The life cycle of static member variables in C # classes is when to create and when to destroy the "lifetime" of a declared element is the time period in which the element is available for use. A variable is the only element with a lifetime, and to do this, the compiler takes process parameters and
The number of return values is considered a special case of a variable. The lifetime of a variable indicates the time period at which it can retain a value. The value of the variable can be changed during the lifetime, but the variable always retains some value.
Survival period:
Instance variable: Both have the same lifetime as the instance of it. When an instance of a class or struct terminates, its non-shared variables lose their value. Each new instance of a class or struct creates all of its non-shared elements and initializes non-shared variables.
Static variables: They are usually present throughout the entire run of the application. The Shared element is persisted until the application stops running.

Class variable: Static variable in memory only one, stored in the method area, belongs to the class variable, is shared by all instances
Destroy: When the class is unloaded, the static variable is destroyed and the memory space is freed. The life cycle of a static variable depends on the life cycle of the class
Class Initialization Order:

Static variable, static code block initialization
constructor function
Custom constructors

When is a static method suitable for use?
1. When this method in your class does not require an object to run, static methods are usually used to define static methods in the tool Class! This way, using the tool class does not create
Object that can be used directly in the class name. Method Name!
For example, now that you have written a method to sort the array (arr), then we define it and other methods as static methods, and the class as a whole can be
As a tool class, assuming that the class name is tools, then you can directly Tools.sort (arr) when you want to use the array ordering method in another class;
Array (of course, the tool class is still imported into the package), instead of having to create objects like the generic class to invoke methods in the class, such as tools
Tools=new Tools (); Tools.sort (arr);

When is a static member variable used more appropriately?

1. Static modifier member Variable: You can use the static modifier if there is data that needs to be shared with all objects.
How static member variables are accessed:
Mode 1: You can use objects for access.
Format: object. Variable name.
Method Two: You can use the class name for access.
Format: class name. variable name;
Attention:
1. Non-static member variables can only be accessed using an object and cannot be accessed using the class name.
2. Do not use static modifier member variables for easy access to data, only use static adornments when the data of member variables is really needed to be shared.
Static Modify the application scenario for member variables: If a data needs to be shared by all objects, the static modifier can be useful at this time.

A summary of learning about static members and methods in C #

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.