Static in C #

Source: Internet
Author: User

Overview

Like in Vb,c#,c,c++,java we can see that static appears as a keyword and function in other advanced computer languages such as Fortran, ALGOL, COBOL, BASIC, LISP, Snobol, PL/1, Pascal, PROLOG , Ada and other languages are also appearing, but have different roles, for its specific role, the reader has the need for the time can be specifically consulted.

Understand

The static keyword is indeed a keyword for the era of process development, but it does not mean that the static keyword should be avoided as much as possible in object-oriented development.

Static class and non-static class effect is certainly not the same, there must be some difference.

The static class is typically used for state-independent classes. So, what is a state-independent class? My understanding is that when there are no attributes in a class, only the method can be considered a class that is not state-independent. Recall that a non-static class instantiates an object, where is the purpose? The purpose is to save the state of the class through this instantiated object.
Isn't it? For example

A = new A (); a.name = "Kevin"; a.sex = "male";

The state of the class is persisted by object A through the object a that we instantiate.
If our class has no attributes and is all methods, then is it necessary to instantiate an object in order to invoke the method in the class?
Therefore, I suggest that, in the design of the class, if the class is a state-independent class, it is recommended that it be designed as a static class.
As for the meaning of this: first, the performance loss of constructing an object is omitted, although it can be neglected. Second, the programmer is more friendly, the method of invoking the class is more convenient.

Summary

As long as a class is a class independent of the State class, it is no problem to design it as a static class.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Static in C #

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.