static void Main(string[] args) { //Example 1 var sampleCollection1 = new SampleCollection1 <string>(); sampleCollection1[0] = "Hello Bill"; Console.WriteLine(sampleCollection1[0]); //Output: Hello Bill //Example 2 var sampleCollection2 = new SampleCollection2 <string>(); sampleCollection2.Add("Hello Bill"); Console.WriteLine(sampleCollection2[0]); //Output: Hello Bill //Example 3 var tempRecord = new TempRecord(); tempRecord[3] = 12.5f; //Using the indexer's set accessor //Using the indexer's get accessor for (int i = 0; i < tempRecord.Lenght; i++) { Console.WriteLine($"Item {i} - Has value: {tempRecord[i]}"); //Output: Item 0 - Has value: 11,1 } //Example 4 var daysCollection = new DaysCollection(); Console.WriteLine(daysCollection["Mon"]); //Output: 0 Console.WriteLine(daysCollection["Fri"]); //Output: 4 Console.WriteLine(daysCollection["WrongDay"]); //Throws ArgumentOutOfRangeException }
static void Main(string[] args) { TempRecord tempRecord = new TempRecord(); // Use the indexer's set accessor tempRecord[3] = 58.3F; tempRecord[5] = 60.1F; tempRecord[1] = 25.1F; tempRecord[4] = 15.5F; TempRecord tempRecord2 = new TempRecord(); // Use the indexer's set accessor tempRecord2[3] = 100.3F; tempRecord2[5] = 200.1F; tempRecord2[1] = 300.1F; tempRecord2[4] = 400.5F; // Use the indexer's get accessor for (int i = 0; i < 10; i++) { System.Console.WriteLine("Element tempRecord #{0} = {1}", i, tempRecord[i]); // System.Console.WriteLine("Element #{0} = {1}", i, tempRecord.Length); System.Console.WriteLine("Element tempRecord2 #{0} = {1}", i, tempRecord2[i]); // System.Console.WriteLine("Element #{0} = {1}", i, tempRecord2.Length); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); //// creating an object of parent class which //// acts as primary address for using Indexer //IndexerCreation ic = new IndexerCreation(); //// Inserting values in ic[] //// Here we are using the object //// of class as an array //ic[0] = "C"; //ic[1] = "CPP"; //ic[2] = "CSHARP"; //Console.Write("Printing values stored in objects used as arrays\n"); //// printing values //Console.WriteLine("First value = {0}", ic[0]); //Console.WriteLine("Second value = {0}", ic[1]); //Console.WriteLine("Third value = {0}", ic[2]); ; }
public static void Main(string[] args) { TempRecord tempRecord = new TempRecord(); // Use the indexer's set accessor tempRecord[0] = 56.2F; tempRecord[2] = 56.5F; // Use the indexer's get accessor for (int i = 0; i < 3; i++) { System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]); } System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); }