InterviewSolution
Saved Bookmarks
| 1. |
What are indexers in C# .NET? What is the difference between property & indexers? |
||||||||||
|
Answer» When we use indexers in class it behaves just LIKE the virtual array. The user sets the value of the index and can retrieve the data without POINTING an instance or a type member. using System; class clsIndexer { private string[] val = new string[3]; public string this[int index] { get { return val[index]; } set { val[index] = value; } } } class main { public static void Main() { clsIndexer ic = new clsIndexer(); ic[0] = "C"; ic[1] = "CPP"; ic[2] = "CSHARP"; Console.Write("Printing values stored in objects used as arrays\n"); Console.WriteLine("First value = {0}", ic[0]); Console.WriteLine("Second value = {0}", ic[1]); Console.WriteLine("THIRD value = {0}", ic[2]); } }
|
|||||||||||