6th. Type and member basis

Source: Internet
Author: User
Tags mathematical functions

In this chapter and subsequent chapters in this section, I will explain how to define different kinds of members in a type to design a type that fits your needs.

6.1 Types of various Members

You can define 0 or more of the following kinds of members in a type:

    • Constant: A constant is a symbol that indicates that the data value is constant. These symbols are often used to make code easier to read and maintain. Constants are usually associated with a type and are not associated with an instance of the type. Logically, constants are always static members.
    • Field: The field represents a read-only or readable writable data value. A field can be static, in which case the field is considered to be part of the type State. A field can also be an instance (non-static), in which case the field is considered part of the object's state. It is strongly recommended to declare a field as private, preventing the state of a type or object from being corrupted by code outside that type.
    • Instance Builder: A special way to initialize an instance field of an object to a good initial state.
    • Type constructor: A special way to initialize a static field of a type to a good initial state.
    • Method: A method is a special function that changes or queries the state of a type or object. When acting on a type, it is called a static method, or an instance method when acting on an object. Method typically performs read and write operations on a field of type or object.
    • Operator overloading: operator overloading is actually a method that defines how the object should be manipulated when a particular operator is applied to the object.
    • Converter overloading: A conversion operator is a method that defines how to transform an object from one type to another implicitly or explicitly.
    • Properties: A simple, field-style syntax for setting or querying part of the logical state of a type or object, while guaranteeing that the state is not compromised, by using a property. The function of a type is called a static property, which acts on an object called an instance property. The property can be either without parameters or with multiple parameters.
    • Events: With static events, a type can send notifications to one or more static or instance methods. With instance events, an object can send notifications to one or more static or instance methods. The event is typically raised when the type of the provided event or the state of the object changes. The event contains two methods that allow static or instance methods to enlist or unregister attention on the event.
    • Type: A type can define other types nested within it. This approach is often used to break down a large, complex type into smaller building blocks (building block) to simplify the implementation.

Whatever programming language you use, its compiler must be able to process your source code, generating metadata and IL code for each member of the list above. Regardless of the programming language used, the format of the metadata is exactly the same. It is because of this feature that the CLR becomes the common language runtime.

The CLR uses the common metadata format to determine the behavior of constants, fields, constructors, methods, properties, and events at run time. Metadata is key to the entire. Net Framework development platform, which enables seamless integration of programming languages, types, and objects.

The following C # code shows a type definition that contains all possible members.

6.2 Types of visibility

When defining types within a file scope, you can specify the visibility of the types as public and internal.

The public type is visible not only to all code in its definition assembly, but also to code in other assemblies.

The internal type is visible only to all code in the defined assembly, not to code in other assemblies.

When defining a type, the C # compiler defaults to setting the visibility of the type to internal (the more limited of the two) if the visibility of the type is not explicitly specified.

Friend Assembly

The Friend Assembly feature is used to access internal members, and private types and private members are still inaccessible.

To enable an assembly (Assembly B) to access the internal types and members of another assembly (assembly a), you should use the InternalsVisibleToAttribute property in assembly a

6.3 Accessibility of Members accessibility

When a member is referenced in code, the accessibility of the member indicates whether such a reference is legitimate.

    • Private: Access is limited to the type that contains the member or to method access in any nested type.
    • Protected: Access is limited to the current class and its subclasses.
    • Internal: Access is restricted to the owning assembly.
    • Internal protected: Access is limited to the current assembly or its subclasses (subclasses may not be part of the current assembly).
    • Public: Access is not restricted.

Of course, any member who wants to be accessed by someone must be defined within a visible type. For example, if an assembly AssemblyA defines a internal type that has a public method, the code in assembly ASSEMBLYB cannot invoke the public method in AssemblyA. Because the internal type is not visible to assemblyb.

In C #, if the accessibility of a member is not explicitly declared, the compiler usually chooses private (the largest limit) by default. The CLR requires that all members of the interface type have public accessibility.

6.4 Static Classes

Static classes are not instantiated, such as the Console,math,environment and ThreadPool classes. These classes have only static members. We directly use its properties and methods, the most common feature of static classes is sharing, the role is to combine a group of related members together. For example, a set of methods for performing mathematical operations is defined in the math class. Math class: Provides constants and static methods for trigonometric, logarithmic, and other general-purpose mathematical functions.

The static keyword can only be applied to a class and cannot be applied to structs (value types). This is because the CLR always allows value types to be instantiated.

The main features of static classes are as follows:

    • They contain only static members.
    • They cannot be instantiated.
    • They are sealed.
    • They cannot contain instance constructors.

The C # compiler restricts static classes to the following:

    • Static classes must derive directly from the base class System.Object, and derive no meaning from any other base class. Inheritance applies only to objects, and you cannot create instances of static classes
    • A static class cannot implement any interface, because only one instance of the class can be used to invoke the class's interface method
    • Static classes can only define static members (fields, methods, properties, and events), and any instance member will cause the compiler to error
    • Static classes cannot be used as fields, method parameters, or local variables because they represent variables that refer to an instance, which is not allowed

Types defined inside a class or struct are called nested types. For example:

class container{    class  Nested    {        Nested () {}}    }

Decorated with the static keyword, which belongs to the class, the instance member belongs to the object, and all static members under this class are loaded when the class is first loaded.

Instance constructors: When you create an object of a class with a new expression, all instance member variables are created and initialized using the instance constructor.

This instance constructor is called whenever an object based on the CoOrds class is created. Constructors such as these without parameters are called "Default constructors". However, it is often useful to provide additional constructors. For example, you can add a constructor to the CoOrds class so that you can specify an initial value for a data member:

classcoords{ Public intx, y; //Constructor     PublicCoOrds () {x=0; Y=0; }     PublicCoOrds (intXinty) { This. x =x;  This. y =fy; }}classmainclass{Static voidMain () {CoOrds P1=NewCoOrds (); CoOrds P2=NewCoOrds (5,3); }}

6.5 Partial classes, structs, and interfaces

Partial this keyword tells the C # compiler that a class, struct, or interface definition source code may be dispersed into one or more source code files.

Splitting a class, struct, or interface type into multiple files is useful when using large projects or automatically generated code, such as code provided by the Windows Forms Designer.

Local types are suitable for the following situations:

    • The type is particularly large and should not be implemented in a file.
    • Part of the code in a type is code generated by the Automation tool and should not be mixed with the code we write ourselves.
    • It takes a lot of people to work on a class.

Note Points for local types:

    • The keyword partial is a contextual keyword that only has the meaning of the keyword when it is put together with class, struct, interface. Therefore, the introduction of partial does not affect variables with the name partial in the existing code.
    • Parts of a local type are generally separated into several different. cs files, but the C # compiler allows us to place them in the same file.
6.6 Component, polymorphic, and version control

Component software Programming (Component software programming)

Some of the features of the components are listed below:

    • Component (. NET is called an assembly) has already been published meaning
    • Component has its own identity (name, version, language culture, and public key)
    • The component always maintains its own identity (the code in the assembly is never statically linked to another assembly.) NET always uses dynamic links)
    • Component clearly indicates which component it depends on (reference metadata table)
    • Component to document its classes and members, the C # language provides this functionality through the source code XML document and the compiler's/doc named line switch
    • The component must specify the security permissions it requires, and the code access security mechanism of the CLR provides this functionality
    • Component to publish an interface that will not change in any "maintenance version"

In. NET, the assembly with the version number 1.2.3.4, with the major version number 1, the minor version number 2, the build number 3, and the revision number 4.

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

Method represents code that performs some action on an instance of a type or type.

Performing an operation on a type is called a static method. Performing an operation on an instance of a type is called a non-static method.

Any method has a name, a signature, and a return value (which can be void).

6th. 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.