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

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

编辑确切的错误是:Bag.cs​​(15,15):错误CS0102:类型Algorithms4e.ch1.Bag<Item>' already contains a definition for项'(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>

关于c# - 尝试添加索引器时如何解决C#编译器错误CS0102?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30293327/

10-09 18:09