Post: (Part 1) in-depth exploration of the object-oriented mechanism of c # Language

Source: Internet
Author: User

Domain
Field, also known as Member Variable, is an indispensable part of the class in C. The domain type can be any data type in C. However, we will not mention other reference types except the string type because some class constructor operations are involved during initialization, we will introduce this part of content as "nesting of classes" in the "interface, inheritance, polymorphism and Class" section.
Domains are divided into instance domains and static domains. The instance domain is a specific object and is private to a specific object. Static fields belong to the class and are shared by all objects. C # Strictly stipulate that instance domains can only be obtained through objects, and static domains can only be obtained through classes. For example, if we have an object of the MyClass type MyObject, The instanceField of the Instance domain in MyClass (the access restriction is public) can only be obtained as follows: MyObject. instanceField. The static field staticField of MyClass (the access restriction is public) can only be obtained as follows: MyClass. staticField. Note that static fields cannot be obtained through objects as traditional C ++ does. That is to say, the usage of MyObject. staticField is incorrect and cannot be compiled by the compiler.
The access restriction of a domain embodies the encapsulation principle of object-oriented programming. According to its protected class C #, the class domain has five different access restrictions:
Public can be accessed at will;
Protected can only be accessed by this class and its inherited sub-classes;
Internal can only be accessed by all classes in the Assembly. The combination is the logical unit and physical unit after the classes in C # language are combined, the compiled file extension is often ". DLL or. EXE ".
Protected internal is a unique composite modifier that can only be accessed by all classes in the current composite and the inherited sub-classes of these classes.
Private can only be accessed by this class.
It can be seen that C # only extends the original friend modifier of C ++ with internal. When it is necessary to make some fields of the two classes visible to each other, we declare the fields of these classes as internal, and then compile them in a composite body. If you need to be visible to their inherited child classes, declare them as protected internal. In fact, this is also the original meaning of the combination-encapsulate the logical-related classes together.
C # introduces the readonly modifier to represent the read-only domain, and const to represent constant. As the name implies, the read-only domain cannot be written, and constant cannot be modified. What is the difference between the two? A read-only domain can only be assigned a value during initialization -- declaring initialization or constructor initialization -- and cannot be assigned to a read-only domain elsewhere. Otherwise, the compiler reports an error. The read-only domain can be an instance domain or a static domain. The type of the read-only domain can be any type of the C # language. However, the const-modified constant must be assigned a value at the same time as it is declared, and the compiler is required to calculate this definite value during compilation. The constant modified by const is a static variable and cannot be obtained by the object. The type of the value modified by const is also limited. It can only be one of the following types (or can be converted to the following types): sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, enum type, or reference type. It is worth noting that the reference type here, except for the string type, all types out of the null value cannot be calculated by the compiler during the compilation period, therefore, the reference type declared as const can only be string or other reference types whose values are null. Obviously, when we declare a null constant, we have lost the meaning of the Declaration-this can be said to be the embarrassment of C # design!

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.