尝试添加索引器时如何解决 C# 编译器错误 CS0102?
How do I resolve C# compiler error CS0102 when trying to add an indexer?
所以我正在尝试将 java Bag class 移植到 C#。我知道我在这里做错了,因为在 foreach 语句中测试这段代码只会产生前两个添加的项目,但这不是我的主要问题。
当我尝试向 Bag class 添加索引器时,编译器抛出 CS0102,抱怨 Bag<Item>
已经包含 Item
的定义。但是我可以创建一个方法 public Item Get(int i)
,它可以很好地完成同样的事情。为什么会发生这种情况,我如何为这个 class 创建索引器?
Edit 确切的错误是:Bag.cs(15,15): 错误 CS0102: 类型 Algorithms4e.ch1.Bag<Item>' already contains a definition for
Item' (CS0102)
作为旁注,我知道 Bag class 不应该使用索引器,但这是事物的原理;我应该能够向任何 class 添加索引器,对吗?
我 运行 Mono C# 编译器版本 3.2.8.0 在 Ubuntu 14.04.2 LTS 下,如果有帮助的话。
如果你们都需要更多信息,或者如果我要发帖,请让我知道这是开始的正确位置。我很乐意更新问题。
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
。
如果您遵循 naming guidlines for type parameters 并将类型参数命名为 TItem
(或只是 T
),则不会 运行 进入此问题。
但如果您确实需要调用类型参数 Item
,您可以使用 IndexerName attribute:
更改索引器的名称 属性
public class Bag<Item> : IEnumerable<Item>
{
...
[IndexerName("MyIndexer")]
public Item this[int i]
{
get
{
return ((ListIterator<Item>)GetEnumerator ())[i];
}
}
...
}
所以我正在尝试将 java Bag class 移植到 C#。我知道我在这里做错了,因为在 foreach 语句中测试这段代码只会产生前两个添加的项目,但这不是我的主要问题。
当我尝试向 Bag class 添加索引器时,编译器抛出 CS0102,抱怨 Bag<Item>
已经包含 Item
的定义。但是我可以创建一个方法 public Item Get(int i)
,它可以很好地完成同样的事情。为什么会发生这种情况,我如何为这个 class 创建索引器?
Edit 确切的错误是:Bag.cs(15,15): 错误 CS0102: 类型 Algorithms4e.ch1.Bag<Item>' already contains a definition for
Item' (CS0102)
作为旁注,我知道 Bag class 不应该使用索引器,但这是事物的原理;我应该能够向任何 class 添加索引器,对吗?
我 运行 Mono C# 编译器版本 3.2.8.0 在 Ubuntu 14.04.2 LTS 下,如果有帮助的话。
如果你们都需要更多信息,或者如果我要发帖,请让我知道这是开始的正确位置。我很乐意更新问题。
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
。
如果您遵循 naming guidlines for type parameters 并将类型参数命名为 TItem
(或只是 T
),则不会 运行 进入此问题。
但如果您确实需要调用类型参数 Item
,您可以使用 IndexerName attribute:
public class Bag<Item> : IEnumerable<Item>
{
...
[IndexerName("MyIndexer")]
public Item this[int i]
{
get
{
return ((ListIterator<Item>)GetEnumerator ())[i];
}
}
...
}