Rose Lilly Jasmine Mango Apple Orange Grapes N. A. N. A. N. A. The Name ORANGE is Found at the Position : 5
Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Program to Implement Overloaded Indexers
C# Program to Implement Overloaded Indexers
This is a C# Program to implement overloaded indexers.
This C# Program Implements Overloaded Indexers.
Here the Indexers are overloaded and the names are displayed.
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(); } } }
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.
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.