C#:study(3)--索引
@索引
一唯索引的一般格式如下:
element-type this[int index]
{
get{...}//返回index指定的值
set{...}//为index指定的对象赋值
}
索引值通过index传递。索引是公共的,允许类以外的代码使用。索引不必即支持get又支持set。可以通过只实现get访问函数的方式来创建只读索引,也可以通过之实现set访问函数的方式来创建只写索引。使用索引有一条重要的限制:因为索引没有定义存储空间,用索引得到的值不能当作ref或out型参数传递给方法。
多维索引形式一般为:
public re-type this[int param1, ..., int paramN]
{
get
{
// process and return some class data
}
set
{
// process and assign some class data
}
}
注意:索引可以被重载,索引的参数可以任何类型。
结果为:
Indexer Output

myInd[0]: empty
myInd[1]: empty
myInd[2]: empty
myInd[3]: Another Value
myInd[4]: empty
myInd[5]: Any Value
myInd[6]: empty
myInd[7]: empty
myInd[8]: empty
myInd[9]: Some Value//一个重载的索引指示器
运行结果为:
Indexer Output
myInd[0]: no value
myInd[1]: no value
myInd[2]: no value
myInd[3]: Another Value
myInd[4]: no value
myInd[5]: Any Value
myInd[6]: no value
myInd[7]: no value
myInd[8]: no value
myInd[9]: Some Value

Number of "no value" entries: 7
一唯索引的一般格式如下:
element-type this[int index]
{
get{...}//返回index指定的值
set{...}//为index指定的对象赋值
}
索引值通过index传递。索引是公共的,允许类以外的代码使用。索引不必即支持get又支持set。可以通过只实现get访问函数的方式来创建只读索引,也可以通过之实现set访问函数的方式来创建只写索引。使用索引有一条重要的限制:因为索引没有定义存储空间,用索引得到的值不能当作ref或out型参数传递给方法。
多维索引形式一般为:
public re-type this[int param1, ..., int paramN]
{
get
{
// process and return some class data
}
set
{
// process and assign some class data
}
}
注意:索引可以被重载,索引的参数可以任何类型。
1
using System;
2
///
3
/// A simple indexer example.
4
///
5
class IntIndexer
6
{
7
private string[] myData;
8
9
public IntIndexer(int size)
10
{
11
myData = new string[size];
12
for (int i=0; i < size; i++)
13
{
14
myData[i] = "empty";
15
}
16
}
17
public string this[int pos]
18
{
19
get
20
{
21
return myData[pos];
22
}
23
set
24
{
25
myData[pos] = value;
26
}
27
}
28
29
static void Main(string[] args)
30
{
31
int size = 10;
32
IntIndexer myInd = new IntIndexer(size);
33
myInd[9] = "Some Value";
34
myInd[3] = "Another Value";
35
myInd[5] = "Any Value";
36
Console.WriteLine("\nIndexer Output\n");
37
for (int i=0; i < size; i++)
38
{
39
Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
40
}
41
}
42
}
using System;2
/// 3
/// A simple indexer example.4
/// 5
class IntIndexer6
{7
private string[] myData;8

9
public IntIndexer(int size)10
{11
myData = new string[size];12
for (int i=0; i < size; i++)13
{14
myData[i] = "empty";15
}16
}17
public string this[int pos]18
{19
get20
{21
return myData[pos];22
}23
set24
{25
myData[pos] = value;26
}27
}28

29
static void Main(string[] args)30
{31
int size = 10;32
IntIndexer myInd = new IntIndexer(size);33
myInd[9] = "Some Value";34
myInd[3] = "Another Value";35
myInd[5] = "Any Value";36
Console.WriteLine("\nIndexer Output\n");37
for (int i=0; i < size; i++)38
{39
Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);40
}41
}42
}
Indexer Output
myInd[0]: empty
myInd[1]: empty
myInd[2]: empty
myInd[3]: Another Value
myInd[4]: empty
myInd[5]: Any Value
myInd[6]: empty
myInd[7]: empty
myInd[8]: empty
myInd[9]: Some Value 1
using System;
2
///
3
/// Implements overloaded indexers.
4
///
5
class OvrIndexer
6
{
7
private string[] myData;
8
private int arrSize;
9
public OvrIndexer(int size)
10
{
11
arrSize = size;
12
myData = new string[size];
13
for (int i=0; i < size; i++)
14
{
15
myData[i] = "empty";
16
}
17
}
18
19
public string this[int pos]
20
{
21
get
22
{
23
return myData[pos];
24
}
25
set
26
{
27
myData[pos] = value;
28
}
29
}
30
31
public string this[string data]
32
{
33
get
34
{
35
int count = 0;
36
for (int i=0; i < arrSize; i++)
37
{
38
if (myData[i] == data)
39
{
40
count++;
41
}
42
}
43
return count.ToString();
44
}
45
set
46
{
47
for (int i=0; i < arrSize; i++)
48
{
49
if (myData[i] == data)
50
{
51
myData[i] = value;
52
}
53
}
54
}
55
}
56
57
static void Main(string[] args)
58
{
59
int size = 10;
60
OvrIndexer myInd = new OvrIndexer(size);
61
myInd[9] = "Some Value";
62
myInd[3] = "Another Value";
63
myInd[5] = "Any Value";
64
myInd["empty"] = "no value";
65
Console.WriteLine("\nIndexer Output\n");
66
for (int i=0; i < size; i++)
67
{
68
Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);
69
}
70
Console.WriteLine("\nNumber of \"no value\" entries: {0}", myInd["no value"]);
71
}
72
}
using System;2
/// 3
/// Implements overloaded indexers.4
/// 5
class OvrIndexer6
{7
private string[] myData;8
private int arrSize;9
public OvrIndexer(int size)10
{11
arrSize = size;12
myData = new string[size];13
for (int i=0; i < size; i++)14
{15
myData[i] = "empty";16
}17
}18

19
public string this[int pos]20
{21
get22
{23
return myData[pos];24
}25
set26
{27
myData[pos] = value;28
}29
}30

31
public string this[string data]32
{33
get34
{35
int count = 0;36
for (int i=0; i < arrSize; i++)37
{38
if (myData[i] == data)39
{40
count++;41
}42
}43
return count.ToString();44
}45
set46
{47
for (int i=0; i < arrSize; i++)48
{49
if (myData[i] == data)50
{51
myData[i] = value;52
}53
}54
}55
}56

57
static void Main(string[] args)58
{59
int size = 10;60
OvrIndexer myInd = new OvrIndexer(size);61
myInd[9] = "Some Value";62
myInd[3] = "Another Value";63
myInd[5] = "Any Value";64
myInd["empty"] = "no value";65
Console.WriteLine("\nIndexer Output\n");66
for (int i=0; i < size; i++)67
{68
Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);69
}70
Console.WriteLine("\nNumber of \"no value\" entries: {0}", myInd["no value"]);71
}72
}
Indexer Output
myInd[0]: no value
myInd[1]: no value
myInd[2]: no value
myInd[3]: Another Value
myInd[4]: no value
myInd[5]: Any Value
myInd[6]: no value
myInd[7]: no value
myInd[8]: no value
myInd[9]: Some Value
Number of "no value" entries: 7




浙公网安备 33010602011771号