. Special type members in net

Source: Internet
Author: User
Tags throw exception

. Special type members in net

. Special type members in net

----Microsoft. NET Platform Series article three

Translation/Zhao Ning

In the previous two articles, we studied the basics of types. In this article we will examine some of the special members that types can define. These types contribute to object-oriented design in terms of greatly simplifying the syntax required for processing types and their object instances.

Type constructor
You are already familiar with what a constructor is, and it is responsible for initializing the state of the object instance. In addition to the instance Builder, Microsoft (R). The net common language runtime (CLR) also supports type constructors (also known as static constructors, class constructors, or type initialization). Type constructors can be applied to interfaces, classes, and numeric types. It allows any member declared in the type to be accessed before the necessary initialization is implemented. The type constructor does not require parameters and always returns a void type. The type constructor accesses only static fields of the type and its usual purpose is to initialize those fields. Before any instances of a type are created and any static fields or methods of a type are referenced, you must ensure that the type constructor is already running.
Many languages, including C #, automatically produce a type constructor when defining a type. However, some languages require an explicit (manual) implementation of the Type Builder.
To understand the type constructor, let's look at the types defined in C #:

Class Atype {
static int x = 5;
}

When this code is built, the compiler automatically generates the Atype type constructor. This constructor is responsible for initializing the static field x to the value 5. If you use ILDASM, it is easy to recognize the type constructor methods because their names are. cctor (for class constructors).
In C #, you can implement a type constructor yourself by defining a static constructor method in a type. The use of the keyword static means that this is the type constructor, not the instance constructor. The following is a very simple example:

Class Atype {
static int x;

Static Atype () {
x = 5;
}
}

This type definition is the same as the previous one. Note A type constructor must never attempt to create its own instance of a type, and the constructor cannot reference a non-static member of the type.
Finally, if you compile the following code with the C # compiler, it produces a separate type constructor method:

Class Atype {
static int x = 5;

Static Atype () {
x = 10;
}
}

The constructor initializes the x=5 first, and then initializes the x=10. In other words, the compiler produces a result type constructor that first contains the initialization code of the static field, followed by the code of the type constructor.

Property
Properties defined by many types can be re-acquired or modified. These properties are often implemented with Type field members. For example, the following is a type definition that contains two fields:

Class Employee {
Public String Name;
Public Int32 age;
}

If you create an instance of this type, it is easy to get or set the property with the following code:

Employee E = new Employee ();
E.name = "Jeffrey Richter"; Setting the Name property
E.age = 36; Set Age attribute

Console.WriteLine (E.name); Show "Jeffrey Richter"

Using properties in this way is very common. But from my point of view, the above code will not be implemented as listed. One of the covenants in object-oriented design and programming is data abstraction. It means that the type field cannot be exposed as a public field because it is too easy to be modified, and it is too easy for someone to write code that improperly uses the field, thus destroying the state of the object. For example, it is easy for someone to write the following code to break the Employee object:

E.age =-5; How can a person's age be-5?

So, when designing a type, I strongly recommend that all fields be private (private) or at least protected (protected)--never public. Then, let the person using the type get or set the property, specifically providing the method for this. The way to package access to a field is called an accessor (or accessor method) method. These methods enable integrity checking at any time and ensure that the object's state is not compromised. For example, I rewrote the employee class previously defined, code one. While this is a simple example, you can understand the great benefits of an abstract data field, and you can see how easy it is to implement a read-only attribute, or simply to achieve a write-only property by not implementing an accessor method.
The data abstraction method shown in figure A has two drawbacks. First, because you want to implement additional functions, write more code. Second, the user of the type must now invoke the method instead of just referencing a single field name:

E.setage (36); Updates The Age
E.setage (-5); Throws an exception

I think all of them will agree that these shortcomings are insignificant compared to their merits, but the runtime still provides a property mechanism that makes the first disadvantage bearable and completely eliminates the second disadvantage.
The classes in Figure II use attributes, which are functionally the same as the classes shown in Figure one. As you can see, properties simplify some code, but it's more important to allow the code to be called as follows:

. Age = 36; Update age
E.age =-5; Throw exception throws an exception

get property accessor is the same as the value type passed to the set property accessor parameter. The return value of the set property accessor is void, and the Get property accessor has no entry parameters. Attributes can be static, virtual, abstract, internal, private, protected, or public. In addition, properties can be defined in the interface, which is discussed later.  
I should also point out that properties do not have to be associated with fields.

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.