Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我会得到;需要一个get或set访问器";在这里_C#_Visual Studio_Oop - Fatal编程技术网

C# 为什么我会得到;需要一个get或set访问器";在这里

C# 为什么我会得到;需要一个get或set访问器";在这里,c#,visual-studio,oop,C#,Visual Studio,Oop,在下面的注释行中,我为什么会得到错误 需要获取或设置访问器 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 名称空间单链接列表 { 班级计划 { 静态void Main(字符串[]参数) { } } 公共类单链接列表 { 私有类节点 { T值; 节点下一步; } 私有节点_root=null; 公共T此[int索引] { for(Node cur=\u root;//指向此

在下面的注释行中,我为什么会得到错误

需要获取或设置访问器

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间单链接列表
{
班级计划
{
静态void Main(字符串[]参数)
{
}
}
公共类单链接列表
{
私有类节点
{
T值;
节点下一步;
}
私有节点_root=null;
公共T此[int索引]
{
for(Node cur=\u root;//指向此处时出错
索引>0&&cur!=null;
--索引,cur=cur.Next);
如果(cur==null)
抛出新的IndexOutOfRangeException();
返回当前值;
}
}
}

您需要指定一个getter:

public T this[int index]
{
    get 
    {
        for(Node cur = _root;
            index > 0 && cur != null; 
            --index, cur = cur.Next);

        if(cur == null)
            throw new IndexOutOfRangeException();

        return cur.Val;
    }
}
关于索引器,请查阅您最喜欢的C语言书籍。这是一个像任何财产一样的财产,只是有一个有趣的名字。属性有一个get和set访问器。
public T this[int index]
{
    get 
    {
        for(Node cur = _root;
            index > 0 && cur != null; 
            --index, cur = cur.Next);

        if(cur == null)
            throw new IndexOutOfRangeException();

        return cur.Val;
    }
}