/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Indexer.cs -- Implements an indexer in a class // // Compile this program with the following command line: // C:>csc Indexer.cs // namespace nsIndexer { using System; public class Indexer { static public void Main () { clsIndexed idx = new clsIndexed (10); Console.WriteLine ("The value is " + idx[3]); idx.Show (3); } } class clsIndexed { public clsIndexed (int elements) { DateTime now = DateTime.Now; Random rand = new Random ((int) now.Millisecond); Arr = new int [elements]; for (int x = 0; x < Arr.Length; ++x) Arr[x] = rand.Next() % 501; } int [] Arr; public int this[int index] { get { if ((index < 0) || (index > Arr.Length)) throw (new ArgumentOutOfRangeException()); return (Arr[index]); } } public void Show (int index) { Console.WriteLine ("The value is " + Arr[index]); } } }