Sixth. Type and member basis

Source: Internet
Author: User

Directory:

6.1 Types of various Members

6.2 Types of visibility

6.3 Visibility of Members

6.4 Static Classes

6.5 Partial classes, structures and interfaces

6.6 Components, polymorphism and versioning

6.1 Types of various Members

Constants : Symbols with constant data values. Solid always with type management, not associated with an instance of the type. Static members are always logical

field : A read-only or readable/writable data value.

Instance Builder: An envoy method that initializes an instance field of an object to a good initial state.

Type Constructor : A special method that initializes a static field of a type to a good initial state.

method : A function that changes or queries the state of a type or object. Acting on a type is called a static method, and acting on an object is called an instance method.

operator Overloading : The actual method that defines how the object should be manipulated when the operator acts on the object.

Conversion Operators : Defines how hermits or displays methods that transform an object from one type to another.

Properties : Allows you to set or query the logical state of a type or object with simple, field-style syntax, while guaranteeing that the state is not compromised.

event : A static event allows a type to send a notification to one or more static or instance methods. Instance events allow an object to send notifications to one or more static or instance methods. Raising an event is typically in response to a change in the type of the provided event or the state of the object. The event contains two methods that allow a static or instance method to enlist or unregister attention on the event. There is also a delegate field to maintain the set of enlisted methods.

type : A type can define additional nested types.

6.2 Types of visibility

Public: Not only visible to code in the definition assembly, but also to code in other assemblies.

internal: visible only to code that defines the assembly.

Friend assembly: enables code access for a type defined as internal to another assembly. The InternalsVisibleTo attribute is available when the assembly is born: This attribute gets the friend assembly name and the string parameter of the public key.

6.3 Visibility of Members
CLR terminology C # Terminology Describe
Private Private Members can only be accessed by a defined type or by any of the nested types of Chinese law
Family Protected A member can only be accessed by a method that defines a type, any nested type, or a derived type regardless of any assembly
Family and Assembly (Not supported) A member can be accessed only by a method in a defined type, any nested type, or any derived type defined in the same assembly
Assembly Internal Members can only be accessed by methods in the definition assembly.
Family or Assembly protected internal A member can be accessed by any nested type, any derived type, or any method in the defined assembly
Public Public Members can be accessed by any method of any assembly

In C #, if the accessibility of a member is not explicitly declared, the compiler typically chooses private by default. The CLR requires that all members of the interface type have public accessibility. The compiler prevents developers from explicitly specifying the accessibility of an interface member, which automatically sets the accessibility of all members to public.

When a derived class overrides a member of a base type definition, the C # compiler requires that the original member and the overriding member have the same accessibility.

6.4 Static Classes

The function of a static class is to combine a group of related members. In C #, you define a class that cannot be instantiated with the static keyword. The class can only be applied to a class and cannot be applied to structs. Because the CLR always allows value types to be instantiated, there is no way to block them.

Limit:

Static classes must derive directly from the base class System.Object, and derive from any other base class is meaningless.

A static class cannot implement any interface, because the class's interface method can be called only when an instance of the class is used.

Static classes can only define static members (fields, methods, properties, and events), and any instance member will cause the compiler to error.

Static members cannot be used as fields, method parameters, or local variables because they represent variables that refer to an instance, which is not allowed.

6.5 Partial classes, structures and interfaces

The partial keyword tells the compiler that the definition of a class, struct, or interface source code may be dispersed into one or more source code files.

There are three reasons to spread the type source code across multiple files:

Source Code Control

Break down into different logical units in the same file or structure

Code splitting

6.6 Components, polymorphism and versioning

component software Programming (Component software PROGRAMMING,CSP) is the culmination of the development of OOP. some of the features of the components are listed below.

A component (the. Net framework is called an assembly) has the meaning of "published".

Components have their own flags (name, version, language culture and public key).

The component always maintains its own flag (the code in the assembly is never statically linked to another assembly;. NET always uses dynamic linking)

The component clearly indicates the component it depends on (referencing the metadata table).

The component should block its classes and members. The C # language provides this common functionality through XML documents in source code and compiler/doc command-line switches.

The component must specify the security permissions it requires. This functionality is provided by the CLR's Code Access Security (SECURITY,CAS) machine.

The interface (object model) that the component will publish in any maintenance version that will not change. Maintenance version represents the new version of the component, which is backward-compatible with the original version of the component.

The version in the. NET Framework contains four parts : Major version number (major version), minor version number (minor version), build number, and revision number (revision). the Major/minor section is usually a sequential, stable set of functions for code assemblies, while the Build/revision section typically represents a maintenance of this feature set.

versioning issues occur when you use a type defined in one component (assembly) as a base type of a type in another component (assembly). Obviously, if the base class's version (modified) is lower than the derived class, the behavior of the derived class also changes, which can cause the class to behave erratically.

C # Keywords related to component versioning

C # Keywords Type Methods/Properties/Events Constants/Fields
Abstract Indicates that an instance of the type cannot be constructed Indicates that in order to construct an instance of a derived type, the derived type must override and implement this member (Not allowed)
Virtual (Not allowed) Indicates that this member can be overridden by a derived type (Not allowed)
Override (Not allowed) Indicates that a derived type is overriding a member of a base type (Not allowed)
Sealed Indicates that the type cannot be used as a base type Indicates that this member cannot be overridden by a derived type, but that the keyword can only be applied to methods that override a virtual method (Not allowed)
New When applied to a nested type, method, property, constant, or field, indicates that the member has no relation to a similar member in the base class

6.6.1 how the CLR calls virtual methods, properties, and events

Compiling the code, the compiler writes a person record entry in the assembly's method definition table, each with a set of flags (flag) that indicates whether the method is an instance method, a virtual method, or a static method.

The compiler that generates the calling code checks the flags of the method definition and determines how the IL code should be generated to invoke the method correctly. The CLR provides two method invocation directives.

Call : the IL directive invokes a static method, an instance method, and a virtual method. To invoke a static method with a call instruction, you must specify the type of the method definition. To invoke an instance method or virtual method with a call instruction, you must specify a variable that references the object. The call instruction assumes that the variable is not NULL. Call directives are often used to invoke virtual methods in a non-virtual manner.

callvirt: the IL directive invokes an instance method and a virtual method, and cannot invoke a static method. To invoke an instance method or a virtual method with the callvirt directive, you must specify the variable that references the object. A non-virtual instance method is invoked with the callvirt directive, and the type of the variable indicates the type of the method definition. Using the CALLVIRT directive to invoke the virtual reality method, the CLR investigates the actual type of the object that emitted the call and then invokes the method in a polymorphic manner. To determine the type, the variable that makes the call must never be null. In other words, when compiling this call, the JIT compiler generates code to verify that the value of the variable is null. If it is, the callvirt instruction causes the CLR to throw a NullReferenceException exception.

The number of virtual methods should be minimized when designing the type. First, calling a virtual method is slower than calling a non-virtual method. Second, the JIT compiler cannot embed virtual methods, which further affects performance. Third, virtual methods make component versioning more vulnerable. Finally, when defining a base type, it is often a convenient way to provide a set of overloads. If you want these methods to be polymorphic, the best approach is to make the most complex methods virtual, making all the convenient methods of overloading a non-virtual method.

6.6.2 the visibility of reasonable usage types and accessibility of members

Advantages of sealed classes with respect to non-hermetic classes:

Version control

Performance

Security and predictability

Principles to follow when defining classes:

When a class is defined, it is always explicitly specified as the sealed class unless it is determined that it is to be used as a base class and allows derived classes to make it special.

Inside the class, define the data field as private.

Inside the class, you define your own methods, properties, and events as priavate and non-virtual. If you want to expose the definition as public, try to avoid defining it as protected or internal, as this will expose the type to a greater security risk. Finally, consider using virtual, because virtual members abandon many controls, lose their independence, and become completely dependent on the correct behavior of the derived type.

OOP has an old adage that when things get too complicated, you get more types. As the implementation of the algorithm begins to become complex, I define some helper types to encapsulate the standalone functionality. If the defined auxiliary type is used by only one "super-type", I will nest these secondary types in "super-type". This allows the code in the nested auxiliary type to refer to the private member defined in the "super-type", in addition to limiting the scope.

6.6.3 processing of virtual methods for version control of types

Override the base class virtual method with override.

Sixth. Type and member basis

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.