-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathIndexers.cs
More file actions
48 lines (45 loc) · 1.17 KB
/
Indexers.cs
File metadata and controls
48 lines (45 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Indexers are used to access elements of a class or struct like arrays.
* It is usually known as smart arrays.
* It is a class property that allows you to access a member variable of a class using the features of an array.
* In C#, indexers are implemented using the this keyword.
*
* Creating an Indexer:
* <modifier> <return type> this[args]
* { get {} set {} }
*/
using System;
namespace Basics
{
class Indexers
{
class IndexerExample
{
private string[] names = new string[10];
public string this[int index]
{
get
{
return names[index];
}
set
{
names[index] = value;
}
}
}
public void Display()
{
IndexerExample names = new IndexerExample();
names[0] = "John";
names[1] = "Doe";
names[2] = "Jane";
names[3] = "Smith";
names[4] = "Alice";
for (int i = 0; i < 5; i++)
{
Console.WriteLine(names[i]);
}
}
}
}