Need for C# indexer method and its implementation

Program: Implemented without indexer Property


using System;

namespace SampleConsoleAppln
{
    class NeedOfIndexer
    {
        class Number
        {
            int[] nums = new int[10];

            public void setValue(int index, int value)
            {
                if (index >= 0 && index < 10)
                {
                    nums[index] = value;
                    Console.WriteLine($"Value {value} is inserved at                                                              index{index}");
                }
                else
                    Console.WriteLine($"Served {value} value can't be                                                            stored at {index}");
            }
            public int getValue(int index)
            {
                if (index >= 0 && index < 10)
                {
                    Console.WriteLine($"Value {nums[index]} is retrieved                                                      from index{index}");
                    return nums[index];
                }
                else
                    Console.WriteLine($"Requesting value can't be                                                     retrieved from index {index}");

                return -1;
            }
        }

        static void Main(string[] args)
        {
            Number nx = new Number();
            nx.setValue(0, 100);
            nx.setValue(1, 200);
            nx.setValue(15,300);
            nx.getValue(0);
            nx.getValue(1);
            nx.getValue(12);
            Console.ReadKey();

            //we can do the same like
            //nx.nums[0] = 100;
            //nx.nums[1] = 200;
            //if it is declared as a public field
            //like public int[] nums = new int[10];
        }
    }
}


Output

Value 100 is inserved at index0
Value 200 is inserved at index1
Served 300 value can't be stored at 15

Value 100 is retrieved from index0
Value 200 is retrieved from index1
Requesting value can't be retrieved from index 12

Program: Implemented with indexer Property


using System;

namespace SampleConsoleAppln
{
    class IndexerExample
    {
        class StringArray
        {
            string[] _ArrX = new string[10];
            public string this[int indexloc]
            {
                set
                {
                    if (indexloc >= 0 && indexloc < _ArrX.Length)
                        _ArrX[indexloc] = value;
                    else
                        Console.WriteLine($"Served {value} value can't be                                                 stored at {indexloc}");
                }
                get
                {
                    if (indexloc >= 0 && indexloc < _ArrX.Length)
                        return _ArrX[indexloc];
                    else
                        Console.WriteLine($"Requesting value can't be                                            retrieved from index {indexloc}");
                    return null;
                }
            }
        }

        static void Main()
        {
            /*Create a new instance to indexer class and assign string                values into array inside the object using object-reference 
            variable. Object reference variable will act as an alias name               to the array and facilitates us to access that simply.*/

            /*Various instances of the indexer class created in many                   client applications consuming indexer class can have 
             names and makes the application development more specific to
             the application domain.*/

            
       //For example, Railway Reservation Application
            //can implement the indexer class like below
            StringArray RailwayStations = new StringArray();
            RailwayStations[0] = "Dindigul (DG)";
            RailwayStations[1] = "Hyderabad (HYB)";
            RailwayStations[2] = "Chennai Egmore (MS)";
            RailwayStations[3] = "KSR Bangalore (SBC)";
            for (int i = 0; i < 12; i++)
            {
                Console.WriteLine(RailwayStations[i] ?? "Not found");
            }

            //For example, Flight Booking Application
            //can implement the indexer class like below
            StringArray Airports = new StringArray();
            Airports[0] = "Madurai Airport (IXM)";
            Airports[1] = "Coimbatore International Airport (CJB)";
            Airports[2] = "Chennai International Airport (MAA)";
            Airports[3] = "Kempegowda International Airport (BLR)";
            Airports[4] = "Rajiv Gandhi International Airport (HYD)";

            for (int i = 0; i < 12; i++)
            {
                Console.WriteLine(Airports[i] ?? "Not found");
            }

            Console.ReadKey();
        }
    }
}

Output

Dindigul (DG)
Hyderabad (HYB)
Chennai Egmore (MS)
KSR Bangalore (SBC)
Not found
Not found
Not found
Not found
Not found
Not found
Requesting value can't be retrieved from index 10
Not found
Requesting value can't be retrieved from index 11
Not found

Madurai Airport (IXM)
Coimbatore International Airport (CJB)
Chennai International Airport (MAA)
Kempegowda International Airport (BLR)
Rajiv Gandhi International Airport (HYD)
Not found
Not found
Not found
Not found
Not found
Requesting value can't be retrieved from index 10
Not found
Requesting value can't be retrieved from index 11

Not found


No comments:

Post a Comment