索引器重载出错 ... 高手看看
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
class huawei
{
private string[] array;
private int dim;
public huawei(int i)
{
dim = i;
array = new string[i];
for (int j = 0; j < i; j++)
{
array[j] = "I am OK !";
}
}
public string this[int i]
{
get
{
return array[i];
}
set
{
array[i] = value;
}
}
public string this[string data]
{
get
{
int j = 0;
for (int i = 0; i < dim; i++)
{
if (array[j] == data)
{
j++;
}
}
return j.ToString();
}
set
{
for (int i = 0; i < dim; i++)
{
if (array[i] == data)
{
array[i] = value;
}
}
}
}
public static void Main(string[] args)
{
int TestNumber = 5;
huawei A = new huawei(TestNumber);
A.array[0] = "Huawei1";
A.array[1] = "Huawei2";
Console.WriteLine("\nINDEX VALUE\n");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("the vales of the array [{0}] is :{1} ", i, A.array[i]);
}
A.array["Huawei1"] = "CHANGE";//错误在这,提示不能把string 强制转换为int.
for (int i = 0; i < 5; i++)
{
Console.WriteLine("the vales of the array [{0}] is :{1} ", i, A.array[i]);
}
Console.ReadKey();
return;
}
}
错误如红字所示 ... 我已经重载了含string参数的索引器,为什么还报错??