C# Examples
Index
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
Create an array of object and Define Indexers in C#
/*To create array of object
Indexers allow you to index a class or a struct instance in the
same way as an array.
Defining an indexer allows you to create classes that act like
"virtual arrays." Instances of that class can be accessed using
the [] array access operator.
*/
using System;
class IndexerClass
{
private int [] myArray = new int[100];
public int this [int index] // Indexer declaration
{
get
{
return myArray[index];
}
set
{
myArray[index] = value;
}
}
}
public class MainClass
{
public static void Main()
{
IndexerClass b = new IndexerClass();
// Call the indexer to initialize the elements #3 and #5.
b[3] = 256;
b[5] = 1024;
for (int i=0; i<=10; i++)
{
Console.WriteLine("Element #{0} = {1}", i, b[i]);
}
}
}
|
Download
Source Code
Index
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