. NET members of the special types

Author:Anonymous    Updated:2008-10-26 21:09:51
In this paper, we will be able to study the type of definition of some of the members of the special. Great efforts to streamline the procedures in the type of object and its example of the need for grammar, which contribute to the type of object-oriented design.

     Type constructor

     You are already familiar with what is the constructor, which is responsible for initialization of the state of the object instance. In addition to the examples other than the constructor, Microsoft (r). NET common language runtime (CLR) also supports the type constructor (also known as a static structure, type of structure or the type of initialization). The type of structure can be applied to the interface, the type of value and type. It allows any type of statement before the visit was a member of the realization of the necessary initialization. Constructor parameters do not need to type and always return to the type of void. Type constructor only visit to the type of static field and its purpose is usually to initialize these fields. In the instance of any type have been created before, as well as any type of static fields or methods have been used before to ensure that has been running for the type constructor.

     Many languages (including C #) in the definition of the type are automatically generated when the type constructor. However, some of the language required explicitly (by hand) to achieve the type constructor.

     In order to understand the type of structure, allow us to study in one of the following C # in the definition of the type:

     class Atype (

     static int x = 5;

     )

     In the establishment of the code, the compiler automatically generated for the Atype type constructor. The constructor to initialize static fields x responsible for the value of 5. If you use ILDasm, can easily identify the type constructor method because their names are. cctor (for the category in terms Constructor).

     In C #, defined by the type of static constructor method, you can achieve the type constructor. Keyword static means that the use of this type of structure, rather than an example constructor. The following is a very simple example:

     class AType (

     static int x;

     static AType () (

     x = 5;

     )

     )

     This type of definition and in front of the same. The type of attention to the constructor must not try to create their own examples of the type and constructor can not be used types of non-members of the static.

     Finally, if you use the C # compiler to compile the following code, which have a separate type constructor:

     class AType (

     static int x = 5;

     static AType () (

     x = 10;

     )

     )

     The first constructor to initialize x = 5, then initialize x = 10. In other words, the compiler the result of first-type structure that contains the code to initialize the static field, followed by the type constructor code .

     Properties

     Many types of properties can be defined or re-amend. These attributes are often used to achieve the type of members of the field. For example, the following is a field that contains two types of definitions:

     class Employee (

     public String Name;

     public Int32 Age;

     )

     If you create this type of case, it is easy to use the following code to be set or attributes:

     Employee e = new Employee ();

     e.Name = "Jeffrey Richter"; / / set the name of property

     e.Age = 36; / / attribute set age

     Console.WriteLine (e.Name); / / show "Jeffrey Richter"

     The use of property in this way is very common. But in my point of view, the code did not set out to achieve was. Object-oriented programming and design of some li is one of the abstract data. It means when the field is the type of field can not be exposed in public, because it was too easy to modify, too easy to write inappropriate use of the field of code in order to break the object of the state. For example, a person can easily prepare the following code to destroy the object Employee:

     e.Age = -5; / / Age is how it -5?

     So, in the type of design, I strongly recommend that all fields are privately owned (private), or at least be protected (protected) - not to the public (public). Then, using the type of person who can Get or Set properties, to provide specialized methods. The field visit to the packing method is called on the access device (or method of access device) method. These methods can achieve the integrity of the inspection at any time and to ensure that the object is not to undermine the state. For example, I had to rewrite the definition in front of the Employee category, a figure code. Although this is a simple example, but you will be able to understand abstract field of the great advantages of the data, from which you can easily understand how to read-only attribute, or just go through the realization of a device to access only way to achieve it easy to write property .

     The data show that there are two shortcomings of the abstract methods. First, because of the need to achieve additional functions, so to write some code. Secondly, the types of users will now have to call the method not only used a single field:

     e.SetAge (36); / / Updates the age

     e.SetAge (-5); / / Throws an exception

     I think all would agree that these shortcomings with its merits than negligible, but the operation still provides a mechanism for property, how many make the first easy to put up with the shortcomings, and the total elimination of the shortcomings of the second.

     The use of the property category, and above its function as shown in the same category. As you can see, some of the properties to simplify the code, but more importantly, to allow the call to write their own, like the following code:

     . Age = 36; / / Update age

     e.Age = -5; / / throw abnormal Throws an exception

     Get access to the property's value and return transfer to the access attribute Set parameters the same type of value. Set access to the property's value is to return void, and does not have access to Get attribute parameters of the entrance. Properties can be static, virtual, abstract, internal, private, protected or public. In addition, the properties can be defined in the interface, will be on the back of the discussion.

     I do not have to attribute it should be noted that in the related fields. For example, type System.IO.FileStream definition of a length of the property, which flow back to the number of bytes. Get the length of the property when the method is called, the length of the field is not provided, but a call to another function request to return to the underlying operating system, open the file stream of bytes.

     When you create a property, the compiler actually issued a special get_ProName and / or set_ProName-access methods (in this case is ProName property). Most of the compiler will understand that these special methods and to allow developers access to these special attribute syntax. However, compliance with the norms common language runtime compiler does not need full support for the property, as long as the dedicated support of the call can access methods.

     In addition, full support for the attributes of the compiler, in the definition and use of property used in a slightly different syntax, for example, with the expansion of managed C + + need to use the keyword _property.

     Property Index

     Certain types, such as logic elements exposed System.Collections.SortedList list. In order to be able to easily access this type of the elements that can define a property index (also known as indexer-indexer). One example of the property index, the index's use of very simple:

     BitArray ba = new BitArray (14);

     for (int x = 0; x <14; x + +) (

     / / All home-even for the "on"

     ba [x] = (x% 2 == 0);

     Console.WriteLine ( "Bit" + x + "is" + (ba [x]? "On": "Off"));

     )

     BitArray example, with an index-Int32 parameters: bitPosition. Indexer with at least one parameter, the number of parameters could be two or more. These parameters (as well as the return type) can be any type. String as a parameter in order to create an index to find joint-array of value is very common. Can provide more than one type of index, as long as the prototype of its different.

     Just set attributes, set-index access method contains a hidden parameter value, when the access method is called, said it wanted a new value. BitArray access to the set parameters of this method shows the value of use.

     A well-designed device should have an index set and get access to the two methods. Even if you can only get access to the realization of methods (for read-only semantics) or set to achieve only method of access (only for the semantic Write), the index suggest that you achieve the two-access device. The reason is simple, users do not want indexed, only half of the act. For example, when the preparation of the following two lines of code, users do not want to see the compiler error:

     String s = SomeObj [5]; / / If there is access, compile OK

     SomeObj [5] = s; / / If there is no access, compile error

     Indexer is always the example of the type of play, and the statement could not be static. However, it is the public, private, protected or internal.

     When you create a property index, the compiler will in fact publish a special get_Item and / or access device set_Item methods. Most of the compiler will understand that these special methods and will allow developers to use a special syntax to access the index attributes of these methods. However, with the CLS (Common Language System)-compatible compiler does not need full support for the property index; as long as the compiler support for the special access device to call.

     Similarly, for full support for the property index compiler in the definition and use of these properties, the need to be minor differences in syntax. For example, C + + by the need to control the expansion of the use of keywords _property.

     Conclusion

     This article discussed the concept of all. NET programmers, it is crucial. I mentioned the specific type of components to enable members of the common language runtime to become the most important. In other words, modern components have been designed to support the property.
Previous:The Visual C # programming WebXML
Next:. NET, on the basis of string to create dynamic controls
User Reviews
Site Search
Recommended article
AD