回 帖 发 新 帖 刷新版面

主题:我这个索引为什么运行不了。

代码如下:
using System;
public class Test  //声明一个数组
{
    public string arr = new string[8];//声明一个8位数的字符串数组
    public string this[int i]   //声明一个索引器,索引器不支持static,索引器只支持实例
    {
        get
        {
            return arr[i];  //返回一个值
        }
        set
        {
            arr[i] = value;  //value是属性值
        }
    }
}
public class MainExample
{
    public static void Main()
    {
        Test strname = new Test();
       strname[1] = "hello";
    } 
}

按照书上的敲出来的。
但是运行了 仔细看了下没发现什么错误。
谁帮我看看谢谢。

回复列表 (共2个回复)

沙发

public string arr = new string[8];//声明一个8位数的字符串数组
改成
public string[] arr = new string[8];//声明一个8位数的字符串数组

板凳

谢了。
看来基础没打好,在去把基础数据类型看一遍。

我来回复

您尚未登录,请登录后再回复。点此登录或注册