2016-07-24 60 views
-2

在下面的注释行,为什么我收到错误为什么我在这里得到“预期的获取或设置访问器”?

get或set访问预计

???

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace SingleLinkedList 
{ 

    class Program 
    { 
     static void Main(string[] args) 
     { 
     } 
    } 

    public class SingleLinkedList<T> 
    { 
     private class Node 
     { 
      T Val; 
      Node Next; 
     } 

     private Node _root = null; 

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

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

      return cur.Val; 

     } 

    } 
} 
+0

咨询您最喜欢的C#语言的书大约索引。这是一个像任何财产一样的财产,只是一个有趣的名字。属性有一个get和set访问器。 –

回答

2

你需要指定一个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; 
    } 
} 
相关问题