在尝试添加索引器时,如何解决c#编译器错误CS0102



所以我试图将java Bag类移植到c#。我知道我在这里做错了,因为在foreach语句中对这段代码的测试只产生了前两个添加的项,但这不是我的主要问题。

当我尝试向Bag类添加索引器时,编译器抛出CS0102,抱怨Bag<Item>已经包含了Item的定义。但是我可以创建一个方法public Item Get(int i),它可以做同样的事情。为什么会发生这种情况,如何为这个类创建索引器?

Edit确切的错误是:Bag.cs(15,15): error CS0102: The type Algorithms4e.ch1.Bag<Item>' already contains a definition for Item' (CS0102)

作为旁注,我知道Bag类不应该使用索引器,但这是事情的原则;我应该能给任何类添加索引器?

我在Ubuntu 14.04.2 LTS下运行Mono c#编译器3.2.8.0版本,如果有帮助的话。

请让我知道如果你们都需要更多的信息,或者如果我张贴这是正确的地方开始。我很乐意更新这个问题。

public class Bag<Item> : IEnumerable<Item> {
    private int N;               // number of elements in bag
    private Node<Item> first;    // beginning of bag
    // helper linked list class
    private class Node<T> {
        public T item;
        public Node<T> next;
    }
    /**
 * Initializes an empty bag.
 */
    public Bag() {
        first = null;
        N = 0;
    }
    /**
 * Is this bag empty?
 * @return true if this bag is empty; false otherwise
 */
    public bool isEmpty() {
        return first == null;
    }
    /**
 * Returns the number of items in this bag.
 * @return the number of items in this bag
 */
    public int size() {
        return N;
    }
    /**
 * Adds the item to this bag.
 * @param item the item to add to this bag
 */
    public void Add(Item item) {
        Node<Item> oldfirst = first;
        first = new Node<Item>();
        first.item = item;
        first.next = oldfirst;
        N++;
    }
    public Item Get(int i)
    {
        return ((ListIterator<Item>)GetEnumerator ())[i];
    }
    public Item this[int i]
    {
        get {
            return ((ListIterator<Item>)GetEnumerator ())[i];
        }
    }
    /**
 * Returns an iterator that iterates over the items in the bag in arbitrary order.
 * @return an iterator that iterates over the items in the bag in arbitrary order
 */
    public IEnumerator<Item> GetEnumerator()  {
        return new ListIterator<Item>(first);  
    }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    // an iterator, doesn't implement remove() since it's optional
    private class ListIterator<T> : IEnumerator<T> {
        private Node<T> current;
        private Node<T> first;
        public ListIterator(Node<T> first) {
            this.first = first;
            current = first;
        }
        public T GetEnumerator() {
            if (!MoveNext ())
                throw new Exception ("No such element");
            T item = current.item;
            //current = current.next; 
            return item;
        }
        public T this[int index]
        {
            get {
                Node<T> temp = first;
                for (int i = 0; i < index; i++) {
                    temp = temp.next;
                }
                return temp.item;
            }
        }
        public T Current
        {
            get { return current.item; }
        }
        object IEnumerator.Current
        {
            get { return Current; }
        }
        public void Dispose()
        {
            current = null;
        }
        public void Reset()
        {
            current = first;
        }
        public bool MoveNext()
        {
            bool res = (current != null);
            current = current.next;
            return (res);
        }
    }
}

如果你想让它通用为什么要有T和Item试着让它都是T

public class Bag<T> : IEnumerable<T>

我认为这就是问题所在…

https://msdn.microsoft.com/en-us/library/a82kxee5%28v=vs.90%29.aspx

你会遇到这种情况,因为索引器的默认属性名称是Item

如果您遵循类型参数的命名准则,并将类型参数命名为TItem(或仅命名为T),您就不会遇到这个问题。

但是如果您确实需要将类型参数命名为Item,您可以使用IndexerName属性更改indexer属性的名称:

public class Bag<Item> : IEnumerable<Item>
{
    ...
    [IndexerName("MyIndexer")]
    public Item this[int i]
    {
        get
        {
            return ((ListIterator<Item>)GetEnumerator ())[i];
        }
    }
    ...
}

相关内容

最新更新