In-depth understanding of static classes and static fields (C # basics)

Source: Internet
Author: User
Preface

In the past, I was always reminded that I should try to use less static variables and data loss during programming. Today I am free and can't help but be curious about static variables, let me share with you the story of the static family.

Static class definition

An important difference between static and non-static classes is that static classes cannot be instantiated.That is to say, you cannot use the new keyword to create a variable of the static class type. When declaring a class, using the static keyword has two meanings: First, it prevents programmers from writing code to instantiate the static class; second, it prevents any instance fields or methods from being declared within the class.

Static classes are introduced only from C #2.0. C #1.0 does not support static class declarations.. The programmer must declare a private constructor. The private constructor prohibits developers from instantiating class instances outside the class scope. The effect of using a private constructor is very similar to that of using a static class.

The difference between a private constructor and a static class: 1) The private constructor can still instantiate classes from inside the class, while the static class cannot instantiate classes from anywhere, this includes the internal class; 2) the class using the private constructor allows instance members, C #2.0 and later compilers do not allow static classes to have any instance members.

The advantage of using static classes is that the compiler can perform checks to ensure that instance members are not accidentally added, and the compiler will ensure that such instances are not created.Another feature of a static class is that the C # compiler automatically marks it as sealed. This keyword specifies a class as non-extensible; in other words, it cannot derive other classes from it.

Main features of static classes:

1. Only static members are included.

2. Unable to instantiate

3. It is sealed

4. The instance constructor cannot be included.

 

 

Lifecycle

When a program that references a static class is loaded, the. NET Framework Common Language Runtime (CLR) loads the type information of the static class. The program cannot specify the exact time for loading the static class. However, you can ensure that the class is loaded before the class is referenced for the first time in a program, initialize the field of the class, and call its static constructor. The static constructor is called only once. during the lifetime of the application domain where the program resides, the static class remains in the memory.

That is to say, when a page is accessed for the first time, it is equivalent to the page being constructed, static variables are initialized by the static constructor, and then accessed again, although non-static resources are re-constructed, however, static resources are not re-constructed and exist all the time.

 

Static member Definition

Non-static classes can contain static methods, fields, attributes, or events. You can call static members in the class even if no instance of the class is created.

You can always access static members by class name instead of Instance name.

No matter how many instances are created for a class, its static members have only one copy.

Static methods and properties cannot access non-static fields and events in their contained types, and cannot access instance variables of any objects (unless explicitly transmitted in method parameters)

Static methods can be overloaded but cannot be overwritten because they belong to a class and do not belong to any instance of the class.

Although the field cannot be declared as static const, the behavior of the const field is static in nature. Such fields belong to the type and do not belong to the type instance. Therefore, you can use classname. membername notation to access the const field like static fields. No object instance is required.

C # static local variables are not supported (variables declared within the method range)

Static FieldShared by all instances of the class, that is, all instances of this class accessThe same memory address.

Static fields have two common usage methods: one is to record the number of instantiated objects, and the other is to store values that must be shared among all instances.

Lifecycle

The static member is initialized before the first access and before calling the static Constructor (if any), and remains in the memory until the lifecycle of the application domain ends.

Summary

Once a static field is used, it will remain in the memory until the end of the life cycle of the program. Therefore, static members occupy memory resources.

If a non-static class contains a static member, the static member remains unchanged no matter how many times the class is created.

In addition, the use of static classes or static members should consider thread security, that is, the occupation of static Resources in the case of multithreading.

Reference: http://www.cnblogs.com/devinzhang/archive/2012/02/09/2344105.html

In-depth understanding of static classes and static fields (C # basics)

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.