320 Object-Based Programming Chapter 8 integer (Space web hosting) value. A

320 Object-Based Programming Chapter 8 integer value. A benefit of indexers is that the programmer can define both integer subscripts and non-integer subscripts. For example, a programmer could allow client code to manipulate data using strings as subscripts that represent the data items names or descriptions. When manipulating conventional C# array elements, the array subscript operator always returns the same data type i.e., the type of the array. Indexers are more flexible they can return any data type, even one that is different from the type of the data in the list of elements. Although an indexer s subscript operator is used like an array-subscript operator, indexers are defined as properties in a class. Unlike normal properties, for which the programmer can choose an appropriate property name, indexers must be defined with keyword this. Indexers have the general form: accessModifier returnType this[ IndexType1 name1,IndexType2 name2, ] { get { // use name1, name2, … here to get data } set { // use name1, name2, … here to set data } } The IndexType parameters specified in the brackets ([]) are accessible to the getand set accessors. These accessors define how to use the index (or indices) to select or modify the appropriate data member. As with properties, get must return a value of type return- Type and set can use the value keyword to reference the value that should be assigned to the data member. Common Programming Error 8.11 Declaring indexers as static is a syntax error. The program of Fig. 8.16 contains two classes class Box (lines 14 74) represents a box with a length, a width and a height, and class IndexerTest(lines 77 177) demonstrates class Box s indexers. 1 // Fig. 8.16: IndexerTest.cs 2 // Indexers provide access to an object’s members via a 3 // subscript operator. 4 5 using System; 6 using System.Drawing; 7 using System.Collections; 8 using System.ComponentModel; 9 using System.Windows.Forms; 10 using System.Data; 11 Fig. 8.16 Fig. 8.1FiFig. 8.16g. 8.16Indexers provide subscripted access to an object s members. (Part 1 of 6.) Fig. 8.16
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Leave a Reply