Implements an indexer and demonstrates that an indexer does not have to operate on an array

image_pdfimage_print

   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Seedr.cs -- Implements an indexer and demonstrates that an indexer
//             does not have to operate on an array
//
//             Compile this program with the following command line:
//                 C:>csc Seed.cs
//
namespace nsIndexer
{
    using System;
    public class Seedr
    {
        static public void Main ()
        {
            clsIndexed idx = new clsIndexed ();
            Console.WriteLine ("The value is " + idx[900]);
        }
    }
    class clsIndexed
    {
        public int this[int index]
        {
            get
            {
                DateTime now = DateTime.Now;
                Random rand = new Random ((int) now.Millisecond);
                return (rand.Next () % index);
            }
        }
    }
}