Understand "static" in C"

Source: Internet
Author: User

I. Static members

1. Modified by the static keyword. It belongs to the class and instance members belong to the object. When this class is loaded for the first time, all static members under this class will be loaded.

2. A static member is created only once. Therefore, there is only one static member, and there are only one copy of each member.

3. When a class is loaded, all static members are created inStatic storage Zone", Once created until the program exits, it will be recycled.

4. When variables need to be shared and methods need to be called repeatedly, these members can be defined as static members.

5. In static methods, instance members cannot be called directly, because objects may not exist when static methods are called.

6. this/base keywords cannot be used in static methods, because the object may not exist.

7. You can create an object of this class and specify that the object members operate in static methods.

8. You can call static members in the instance method, because static members certainly exist at this time.

Ii. Differences between static and instance members

1. different lifecycles.

2. The storage locations in the memory are different.

Iii. Static class

1. Class modified by the static keyword.

2. Only static members can be declared in the static class.

3. The essence of static classes is an abstract and sealed class, so they cannot be inherited or instantiated.

4. If all the members under a class need to be shared, you can define this class as a static class.

Iv. Static Constructor

1. A Member of this class will execute a static constructor before being accessed for the first time.

2. The static constructor is executed only once.

 

Eg:

              i =                       i =              Console.Write(                        i =              Console.Write(            Main(              Console.Write(Program.i);             Program p =              Console.Write(Program.i);      }

 

Note: All the above articles are original. If you need to reprint them, please indicate the source!

 

 

 

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.