[Study Notes] C # primer Chinese edition-class design, static members, const and readonly data members

Source: Internet
Author: User
Class composition: public operations and properties; non-public implementation details.
Class Design: 1. Determine the set of operations that can be executed by the class. These operations will become member functions of the class.
2. Determine the interface (return type, parameter column) of each member function ).
3. determine the access level of each function.

static data members only have one unique entity in the entire Program . The syntax for accessing static members in the class member function is exactly the same as that for accessing instance members. In addition to the class ontology, we can directly access the object by using the Class Name of the static member. Note: Therefore, you cannot access static members using an object of the class.
at most one static constructor can be defined for each class, and access-level keywords cannot be added. The parameter column must be blank.
in order not to violate the encoding rules, static data members are usually declared as private, and read/write access is provided through statci property.

definition: The const keyword is used to modify the declaration of a field or local variable. The value of the specified field or local variable cannot be modified. Constant declaration introduces one or more constants of a given type.
the const data member declaration must contain the initial value, and the initial value must be a constant expression. Because it needs to be fully evaluated during compilation.
a const member can be initialized using another const member, provided that there is no circular dependency between the two.
readonly evaluates and assigns values at runtime, so that we can postpone object initialization until runtime while ensuring "read-only access.
the readonly keyword is different from the const keyword. The const field can only be initialized in the declaration of this field. The readonly field can be initialized in the Declaration or constructor. Therefore, the readonly field may have different values based on the constructors used. In addition, the const field is the number of compiling times, while the readonly field can be used for running times.

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.