在尝试添加索引器时,如何解决C#编译器错误CS0102?
所以我尝试将一个java类移植到C#。我知道我在这里做得不对,因为在foreach语句中测试这段代码只会生成前两个添加项,但这不是我的主要问题在尝试添加索引器时,如何解决C#编译器错误CS0102?,c#,generics,compiler-errors,mono,indexer,C#,Generics,Compiler Errors,Mono,Indexer,所以我尝试将一个java类移植到C#。我知道我在这里做得不对,因为在foreach语句中测试这段代码只会生成前两个添加项,但这不是我的主要问题 public class Bag<Item> : IEnumerable<Item> { private int N; // number of elements in bag private Node<Item> first; // beginning of bag
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);
}
}
}
当我试图向Bag类添加索引器时,编译器抛出了一个CS0102,抱怨Bag
已经包含了项的定义。但是我可以创建一个方法public Item Get(inti)
,它可以很好地完成同样的事情。为什么会发生这种情况?我如何为这个类创建索引器
编辑确切的错误是:Bag.cs(15,15):错误CS0102:typeAlgorithms4e.ch1.Bag'已经包含了
项的定义(CS0102)
作为旁注,我知道Bag类不应该使用索引器,但这是它的原理;我应该可以为任何类添加索引器,对吗
我正在Ubuntu 14.04.2 LTS下运行Mono C#compiler 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);
}
}
}
公共类包:IEnumerable{
private int N;//包中的元素数
private Node first;//包的开头
//助手链表类
私有类节点{
公共交通项目;
公共节点下一步;
}
/**
*初始化空包。
*/
公文包(){
第一个=空;
N=0;
}
/**
*这个包是空的吗?
*@如果此包为空,则返回true;否则返回false
*/
公共图书馆是空的{
返回first==null;
}
/**
*返回此包中的项目数。
*@返回此包中的物品数量
*/
公共整数大小(){
返回N;
}
/**
*将项目添加到此包中。
*@param item要添加到此包中的项目
*/
公共作废添加(项目){
节点oldfirst=第一个;
第一个=新节点();
first.item=项目;
first.next=oldfirst;
N++;
}
公共项目获取(int i)
{
返回((ListIterator)GetEnumerator())[i];
}
公共项目本[int i]
{
得到{
返回((ListIterator)GetEnumerator())[i];
}
}
/**
*返回一个迭代器,该迭代器按任意顺序遍历包中的项目。
*@返回一个迭代器,该迭代器以任意顺序对包中的项目进行迭代
*/
公共IEnumerator GetEnumerator(){
返回新的ListIterator(第一个);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
//迭代器不实现remove(),因为它是可选的
私有类ListIterator:IEnumerator{
专用节点电流;
私有节点优先;
公共ListIterator(节点优先){
this.first=first;
电流=第一;
}
公共T GetEnumerator(){
如果(!MoveNext())
抛出新异常(“无此类元素”);
T项目=当前项目;
//当前=当前。下一步;
退货项目;
}
公共T此[int索引]
{
得到{
节点温度=第一;
对于(int i=0;i
如果你想让它成为通用的,为什么你有T,试着把它全部变成T
public class Bag<T> : IEnumerable<T>
公共类包:IEnumerable
如果你想让它成为通用的,为什么你有T,试着把它全部变成T
public class Bag<T> : IEnumerable<T>
公共类包:IEnumerable
如果你想让它成为通用的,为什么你有T,试着把它全部变成T
public class Bag<T> : IEnumerable<T>
公共类包:IEnumerable
如果你想让它成为通用的,为什么你有T,试着把它全部变成T
public class Bag<T> : IEnumerable<T>
公共类包:IEnumerable
我相信这就是问题所在
我相信这就是问题所在
我相信这就是问题所在
我相信这就是问题所在
您遇到这种情况是因为索引器的默认属性名是项
如果您遵循并命名了类型参数TItem
(或者只是T
),就不会遇到此问题
但如果确实需要调用类型参数项
,则可以使用以下命令更改索引器属性的名称:
公共类包:IEnumerable
{
...
[索引器名称(“MyIndexer”)]
公共项目本[int i]
{
得到
{
返回((ListIterator)GetEnumerator())[i];
}
}
...
}
您遇到这种情况是因为索引器的默认属性名是项
如果您遵循并命名了类型参数TItem
(或者只是T
),就不会遇到此问题
但如果确实需要调用类型参数项
,则可以使用以下命令更改索引器属性的名称:
公共类包:IEnumerable
{
...
[索引器名称(“MyIndexer”)]
公共项目本[int i]
{
得到
{
返回((ListIterator)GetEnumerator())[i];
}
}
...
}
您的