Summary

   


In this chapter, you learned about the following function class members: properties, indexers, user-defined operator overloading, and user-defined conversions. We also looked at another kind of class member called nested types. The most important points in this chapter are noted in the following paragraphs.

Properties are better suited than accessor and mutator methods to access instance variables. The property is accessed in the same way as a public instance variable, but when a value is assigned or retrieved via the property, programmer defined set statement blocks and get statement blocks are executed. A property does not have to represent any particular instance variable but can merely return a calculated value.

Properties are as fast as accessor and mutator methods, because properties and methods look very similar when compiled into MSIL. Under the right circumstances, properties become as fast as directly accessing the instance variable. This is accomplished through an optimization technique called inlining.

If an instance variable is resource demanding to initialize and update, you can use properties together with two techniques called delayed initialization and lazy updates to delay and reduce the number of initializations and updates required.

Indexers allow you to access the data inside the array of an object by applying an array-like syntax (square brackets enclosing an index) to the object name itself.

The keyword this can be used to call an indexer from within the object in which the indexer is defined.

Several indexers can be included in the same class if they have different signatures. They are then called overloaded indexers.

User-defined operator overloading allows you to, for example, specify the + operator to perform your own tailor-made operation on the objects of a user-defined class, and thereby extend the + operator's use beyond the predefined numerical addition and string concatenation. C# only allows you to overload a restricted set of operators and does not permit you to create your own operators (such as ^*).

User-defined conversions let you extend the predefined implicit and explicit conversion paths already defined for the simple types to include your own user-defined types.

It is easy to overuse properties, indexers, operator-overloading, and user-defined conversions. Each construct has its own rules for when it is appropriately applied. However, there is one general question you should always ask yourself before implementing any of the constructs, "Viewed from the class users point, does the inclusion of the construct make the class more intuitive or more cryptic to use?" Only go ahead if you are convinced the answer is more intuitive.

C# allows you to insert the definition of a type (class, enum, interface) into a class block. They help organize your types and reduce the number of types through which programmers inspecting your code for reuse purposes need to sift.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net