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;
}
}