Indexers - Features,Properties - Csharp.Net
Q. Which of the following statements are correct?
1. Indexers enable objects to be indexed in a similar manner to arrays.
2. The this keyword is used to define the indexers.
3. Indexers can be overloaded.
4. Indexer cannot be used in interface
- Published on 31 Aug 15a. 1, 2
b. 3
c. 1,2,3
d. None of the above
ANSWER: 1,2,3
The indexer has following properties.
• Indexers enable objects to be indexed in a similar manner to arrays.
• A get accessor returns a value. A set accessor assigns a value.
• The this keyword is used to define the indexers.
• The value keyword is used to define the value being assigned by the set indexer.
• Indexers can be overloaded.
Example:
classIndDemo
{
privateint [ ] arr = new int[3];
publicint this[int i]
{
get
{
returnarr[i];
}
set
{
arr[i] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
IndDemoobj = new IndDemo();
obj[0] = 10;
Console.WriteLine(obj[0]);
}
}