郁闷的索引器
我定义了一个类,有两个私有一维数组,然后这个时候要使用索引器,悲惨的一幕就发生了。
没查MSDN,具体要怎么写,先用了个蹩脚的方式解决了,不知道正常要怎么写,就请各位跟贴咯。
看代码:
1 public class Game
2 {
3 private int[] kindID = new int[10];
4 private int[] serverID = new int[10];
5
6 public int this[int kIndex,int sIndex]
7 {
8 get {
9 if (kIndex == 11)
10 {
11 return serverID[sIndex];
12 }
13 else
14 return kindID[kIndex];
15 }
16 set {
17 if (kIndex == 11)
18 {
19 serverID[sIndex] = value;
20 }
21 else
22 kindID[kIndex] = value;
23 }
24 }
25 }
2 {
3 private int[] kindID = new int[10];
4 private int[] serverID = new int[10];
5
6 public int this[int kIndex,int sIndex]
7 {
8 get {
9 if (kIndex == 11)
10 {
11 return serverID[sIndex];
12 }
13 else
14 return kindID[kIndex];
15 }
16 set {
17 if (kIndex == 11)
18 {
19 serverID[sIndex] = value;
20 }
21 else
22 kindID[kIndex] = value;
23 }
24 }
25 }
然后就是这个样子:
static void Main(string[] args)
{
Game game = new Game();
game[0, 11] = 111;
game[11, 0] = 222;
Console.WriteLine(game[0, 11].ToString());
Console.WriteLine(game[11, 0].ToString());
Console.ReadLine();
}
{
Game game = new Game();
game[0, 11] = 111;
game[11, 0] = 222;
Console.WriteLine(game[0, 11].ToString());
Console.WriteLine(game[11, 0].ToString());
Console.ReadLine();
}
越看越恶心,各位是怎么写的?