C # Static,

Source: Internet
Author: User

C # Static,
Background

  • Static Source

When writing a class, sometimes a member in the class needs to be unique, that is, to maintain only one State for all objects. For example, if we create personal information, we are all Chinese, but we cannot save a Chinese field for everyone. It is enough to use one..

  • Static Function

The static modifier is used to declare a type rather than a static member of a specific object. That is to say, objects modified using static are all members of a class and are not unique to a class. For example, a car has four wheels, rather than a car with four wheels.

  • Static features

1. class objects do not need to be declared during access. For example

Console. Write (); // Write () This is a static method and does not require new mongolo ();

2. static objects are unique. No matter whether an object is generated or no matter how many objects are generated, some specific data has only one copy in the memory space. That is to say, no matter how many new class members you have, there is only one static variable that needs to be changed.

 

 

Static Field

The so-called static field is a field with static modification..

  • Function

No matter how many objects are created, the static field indicates that there is only one data in the memory space. For example, no matter how many software you have installed and want to know what time it is, there is only one reference value. If you change this time, the time that other software gets has changed. (Of course, this is just an example. I don't care about the access time ).

  • Operation Sequence

Static fields are the first objects to be instantiated during class operations (such as instantiation and calling of static methods. The second is the static constructor, and both of them can be called only once.

  • Features

 
  • Example

 

For example, each time an instance is created, the constructor performs + 1 on the two fields, but the results are different. This is the uniqueness of the static modified field.

 

 

Static Constructor
  • Function

Static constructor is generally used to initialize static variables without initializing non-static variables. However, because non-static constructor can also operate static variables, static constructor is rarely used.

 

  • Operation Sequence

No matter what type of operation is performed on the class, the static constructor is always the first method to be operated except the static field, which is earlier than the non-static parameters and non-static constructor.

  • Features
 

 

  • Example

 

class A    {        public static int X;        static A()        {            X = B.Y + 1;        }    }    class B    {        public static int Y = A.X +1;        static B()        {        }        static void Main(string[] args)        {            Console.WriteLine(string.Format("Y={0}, X={1}", B.Y, A.X));//Y=2, X=1;            Console.ReadLine();                  }    }

 

This is a well-known static example on the Internet. If you don't look at the answer, you can calculate it.

I can describe the complete sequence. static interview questions cannot help you!

 

 

Static Method
  • Application

Static methods are generally used in scenarios where frequent calls are not frequently modified. For example, the tool library method, the method that does not need to be instantiated, the method is irrelevant to the specific business, the execution is frequent, and there are many methods in use. Of course, this feature is also because static methods cannot be inherited and cannot be extended.

  • Operation Sequence

Static methods can be called only after static fields and static constructors are operated.

  • Features

 

 

Answer questions
  • What is the biggest difference between static variables and non-static variables?

 

 

The biggest difference is the memory location. Static variable memory is used when the program starts to execute the variable memory, until the end of the entire program to release the amount of memory. non-static variables are allocated when the program runs to this step and are automatically destroyed after execution. Therefore, the value of the static variable is initialized only once, and each subsequent access is the value previously processed.

 

 

  • Why can I access static variables without instantiation.

The reason is that the class has allocated the memory space for the static variables during initialization, and the location of the static variables can be accurately found without instantiation. Non-static members must be after the class object is instantiated, to determine their memory locations. Therefore, static members belong to Classes, rather than static members belong to class objects.

  • Why can't I access non-static variables or create non-static variables in static methods?

 

Because non-static variables need to be instantiated before they can be called by confirming the location in the memory, but static functions do not need to be instantiated, so static methods cannot be used.

 

  • Why can't static variables be declared inside the method?

Because staticVariableIs shared by all instances of the class.VariableIs a global variable in the category of this class. If static variables are defined within the method, the static variables defined are local variables, which naturally leads to errors.

 

  • Lifecycle of static members

The lifecycle of a static member is the lifecycle of the entire application domain, that is, from the operation of a class containing static members to the end of the entire application.

 

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.