To: C #: Role of the static keyword,

Source: Internet
Author: User

To: C #: Role of the static keyword,

Tatic indicates static, and can be used to modify classes, fields, attributes, and methods.

If it is marked as static, you do not need to create an instance object for calling. You can click the class name directly.

StaticThree usage methods:

1.Before a variableIndicates that every time you re-use the method, class, or custom class where the variable is located,The value of the variable is the value when the last value is assigned to the variable during this running of the program.This method is calledStatic Functions:

Private void s ()

{

Static int a = 1;

A ++;

}

The value of a in-memory after the first call is completed is 2;

The value of a in-memory is 3 after the first call;

 

2. Use static modification before a method (function) to indicate that this method belongs to the class or custom class, rather than the instance of this class. This method is calledStatic Method:

Case 1: non-static method:

Class t

{

T (... (parameter, the same as below ))

{

~~~~ (Method content, the same as below)

}

Void s (....)

{

~~~~

}

}

When you call Method s in this class in other methods, you must first declare the variables of this class, for example, t sd = new t (....);

Call the method in t: sd. s (....);

Case 2: static method:

Class t

{

T (... (parameter, the same as below ))

{

~~~~ (Method content, the same as below)

}

Static void s (....)

{

~~~~

}

}

When you call Method s in this class in other methods, you do not need to declare the variables of this class first, for example, directly calling the method: t. s (....);

Well, this depends on your specific needs. We know that after the static keyword is declared before variables, methods, and classes, they are global,
The advantage is that you do not need to frequently use the new Keyword of the pitfalls when using some methods (to be honest, this is really troublesome ).
The other is to consider the security of system resources. For example, for employees, a company needs a lot of employees, so we can create countless new employees, but it is best for department leaders to be static, in this way, many new leaders will be prevented!

 

3. before using this class, it means that the instance cannot be created. Simply put, all the methods of this type are static methods, and non-static methods in this class are not usable. This type is calledStatic class.

For example, the Members in the Cancle class operated on the C # console are marked as static. You can directly use Concle.Click to use.

If it is not marked as static, it is called by creating an instance object. For example, the dynamic string StringBuilder requires a new instance to call

StringBuilder sb = new StringBuilder ();

Sb. xxx (); // xxx is the method name

 

 

Static class t

{

~~~~

}

~~~~

Class d

{

~~~~

Void f (....)

{

~~~~

T v = new t (); // an error occurs in the program.

}

}

Tips:

The entire type is loaded only once during the program running. This is for frequently used types, so you do not need to load them before each use. High efficiency.

However, for infrequently used types, if the static type is used, it will occupy a considerable amount of memory until the program stops. Or the application domain is unloaded. Therefore, only common types should be defined as static types.

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.