C# Program to Implement Overloaded Indexers

This is a C# Program to implement overloaded indexers.

Problem Description

This C# Program Implements Overloaded Indexers.

Problem Solution

Here the Indexers are overloaded and the names are displayed.

Program/Source Code

Here is source code of the C# Program to Implement Overloaded Indexers. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement Overloaded Indexers
 */
using System;
namespace IndexerApplication
{
    class IndexN
    {
        private string[] list = new string[size];
        static public int size = 10;
        public IndexN()
        {
            for (int i = 0; i < size; i++)
            {
                list[i] = "N. A.";
            }
        }
        public string this[int index]
        {
            get
            {
                string tmp;
 
                if (index >= 0 && index <= size - 1)
                {
                    tmp = list[index];
                }
                else
                {
                    tmp = "";
                }
 
                return (tmp);
            }
            set
            {
                if (index >= 0 && index <= size - 1)
                {
                    list[index] = value;
                }
            }
        }
        public int this[string name]
        {
            get
            {
                int index = 0;
                while (index < size)
                {
                    if (list[index] == name)
                    {
                        return index;
                    }
                    index++;
                }
                return index;
            }
 
        }
 
        static void Main(string[] args)
        {
            IndexN names = new IndexN();
            names[0] = "Rose";
            names[1] = "Lilly";
            names[2] = "Jasmine";
            names[3] = "Mango";
            names[4] = "Apple";
            names[5] = "Orange";
            names[6] = "Grapes";
            //using the first indexer with int parameter
            for (int i = 0; i < IndexN.size; i++)
            {
                Console.WriteLine(names[i]);
            }
            //using the second indexer with the string parameter
            Console.WriteLine("The Name ORANGE is Found at the Position : "+
                               names["Orange"]);
            Console.ReadKey();
        }
    }
}
Program Explanation

This C# program is used to implements overloaded indexers. Using the first indexer with int parameter declare the elements using for loop. Then using the second indexer with the string parameter overloads the indexers.

advertisement
advertisement

In this[] procedure if else condition statement is used to check the value of ‘index’ variable is greater than or equal to 0 and the value of ‘index’ variable is less than or equal to size using logical OR operator. If the condition is true then execute the statement and assign the value of ‘element’ to ‘tmp’ variable.

Otherwise, if the condition is false then execute the else statement assign the empty value to ‘tmp’ variable. The this[] procedure is used to implement overloaded indexers value. While loop is used to check the condition, that the value of ‘index’ variable is less than the value of ‘size’ variable.

If the condition is true, then execute the iteration of the loop. If condition statement is used to check the condition that the value of list[] array variable is equal to the value of ‘name’ variable. If the condition is true then execute the statement. Hence the indexers are overloaded and print the names.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
Rose
Lilly
Jasmine
Mango
Apple
Orange
Grapes
N. A.
N. A.
N. A.
The Name ORANGE is Found at the Position : 5

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.