Access Control Character static for beginners

Source: Internet
Author: User

C # modifier: static

The Declaration belongs to the type owner rather than the Member of the object.

I. Non-static class

1. Non-static classes can contain static methods, fields, attributes, or events. Static class members belong to the class level and do not belong to any instance of the class. Static variables cannot be called by Class Object outside the class. They must be called by class name, (For example, there is a Student class, which has a static string name. When other classes need to be called, Student is used. name); no matter how many instances are created for a class, its static members have only one copy, and all the Members in the class can access it and change its value. Static methods and attributes 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 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.

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.

2. The static member is initialized before the first access and if a static constructor is called. A static constructor is used to initialize any static data or perform a specific operation that only needs to be performed once. The static constructor is automatically called before the first instance is created or any static member is referenced.

Public class Students

{

// Define static variables

Public staic int Num;

// Static Constructor

Static Students ()

{

Num = 30;

}

}

3. Static constructor has the following features:

The static constructor neither has an access modifier nor a parameter. It is used to initialize static and read-only fields.

Before creating the first instance or referencing any static member, the class is automatically initialized by calling the static constructor. Class static constructor can be executed at most once in a given application domain.

Static constructors cannot be inherited and cannot be called directly.

In a program, you cannot control when to execute static constructors.

If a static constructor is not compiled, and the class contains a static field with an initial value, the compiler automatically generates the default static constructor.

4. Description of static variables:

1) static methods and static data members are equivalent to shared variables, with only one copy shared by all objects in the class. Therefore, when data needs to be shared, A good choice for defining this type. Once a class is defined, it is not necessary to instantiate the object.) All static members of the class are loaded into the memory (not when the program is started, but not when the class is not defined) the scope of a static member is the same as that of its class.

2) The static variable value inside the method does not disappear after the static variable is executed. When the method of this object is re-executed, the value still exists and is not allocated in the stack, it is analyzed in the static area. There are two types of variables stored in the static storage area: global variables and static variables.) This is the biggest difference with local variables.

3) Because static member functions exist at the class level and are not part of an object, there is no this pointer. It is wrong to reference this in a static method.

Ii. Static class:

Declared as static. It only contains static members and cannot use new static class instances. Static classes are used to include methods that are not associated with specific objects.

Function: contains only static members and cannot be instantiated. It is sealed and cannot contain instance constructors. It can contain static constructors to allocate initial values or set a static variable.

Advantage: the compiler can perform checks to ensure that force members are not accidentally added. The compiler will never create such instances.

 

 

 

 

 

This article is from the "branches" blog, please be sure to keep this source http://xgcxz99gm.blog.51cto.com/7227172/1266043

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.