我在索引中聲明瞭兩個私有數組,並在main中顯示數據。但是,它沒有顯示任何一個告訴我如何在索引器中的兩個私有數組中顯示數據?通過在c中使用索引器在私有數組中排列數據#
class Program
{
static void Main(string[] args)
{
student sc = new student();
for (int i = 0; i < sc.mlength; i++)
{
Console.WriteLine(sc[i]);
}
Console.ReadLine();
//i am declaring two private arrays in indexes and displaying the data in main is not displaying any one tell me how to display the data in the two private arrays in indexers?
}
}
public class student
{
private int[] _marks = new int[] { 60, 68, 70 };
private string[] _names = new string[] { "suri", "kumar", "suresh" };
public int this[int i]
{
get
{
return _marks[i];
}
set
{
_marks[i] = value;
}
}
public string this[int i]
{
get
{
return _names[Convert.ToInt32(i)];
}
set
{
_names[Convert.ToInt32(i)] = value;
}
}
public int mlength
{
get
{
return _marks.Length;
}
}
public int nlenght
{
get
{
return _names.Length;
}
}
}
}
你目前正試圖聲明兩個具有相同參數的索引器 - 它不應該編譯。請特別注意你所遇到的錯誤 - 「不顯示」並不夠具體。 (我還強烈建議您閱讀.NET命名約定。) –