因此,我试图将一个java 袋类移植到C#。我知道我在这里做错了,因为在foreach语句中对这段代码的测试只产生前两个添加的项,但这不是我的主要问题。
当我试图向包类添加索引器时,编译器会抛出一个CS0102,抱怨Bag<Item>
已经包含了Item
的定义。但是我可以创建一个方法public Item Get(int i)
,它可以做同样的事情。为什么会发生这种情况,以及如何为该类创建索引器?
编辑准确的错误是: Bag.cs(15,15):error CS0102:类型Algorithms4e.ch1.Bag<Item>' already contains a definition for
Item‘(CS0102)
顺便提一句,我知道包类不应该使用索引器,但这是事情的原理;我应该能够在任何类中添加索引器,对吗?
我正在Ubuntu14.04.2LTS下运行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);
}
}
}
发布于 2015-05-18 00:01:05
如果你想使它成为通用的,为什么你有T和物品,试着把它变成全T
public class Bag<T> : IEnumerable<T>
发布于 2015-05-18 00:15:54
发布于 2015-05-18 14:50:14
您会遇到这种情况,因为索引器的默认属性名是Item
。
如果您遵循类型参数命名指南并将类型参数命名为TItem
(或仅命名为T
),则不会遇到此问题。
但是,如果确实需要调用Item
的类型参数,则可以使用IndexerName属性更改索引器属性的名称。
public class Bag<Item> : IEnumerable<Item>
{
...
[IndexerName("MyIndexer")]
public Item this[int i]
{
get
{
return ((ListIterator<Item>)GetEnumerator ())[i];
}
}
...
}
https://stackoverflow.com/questions/30293327
复制相似问题