//程序清单P14_1.cs:
using System;
using System.Collections.Generic;namespace P14_1
{
public class AssembleIteratorSample
{
static void Main()
{
Assemble<int> iAsm = new Assemble<int>();
iAsm.Add(4);
iAsm.Add(12);
iAsm.Add(9);
iAsm.Add(1);
iAsm.Add(8);
Console.WriteLine("排序前:");
foreach (int i in iAsm)
Console.WriteLine(i);
Console.WriteLine("排序后:");
foreach (int i in iAsm.SortedEnumerator())
Console.WriteLine(i);
}
} /// <summary>
/// 泛型类: 集合Assemble
/// </summary>
public class Assemble<T> : IEnumerable<T>
where T : IComparable<T>
{
//字段
protected T[] m_list;
protected int m_count = 0;
protected int m_capacity = 100; //属性
public int Length
{
get
{
return m_count;
}
} public int Capacity
{
get
{
return m_list.Length;
}
} //索引函数
public T this[int index]
{
get
{
return m_list[index];
}
set
{
m_list[index] = value;
}
} //构造函数
public Assemble()
{
m_list = new T[m_capacity];
} public Assemble(int iCapacity)
{
m_capacity = iCapacity;
m_list = new T[m_capacity];
} //方法
public void Add(T tp)
{
if (m_count < m_list.Length)
m_list[m_count++] = tp;
} public void Remove(T tp)
{
int index = this.Find(tp);
if (index == -1)
return;
this.RemoveAt(index);
} public void RemoveAt(int index)
{
for (int i = index; i < m_count - 1; i++)
{
m_list[i] = m_list[i + 1];
}
m_list[--m_count] = default(T);
} public int Find(T tp)
{
for (int i = 0; i < m_count; i++)
if (m_list[i].Equals(tp))
return i;
return -1;
} public void Sort()
{
T tmp;
for (int i = m_count - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (this[j].CompareTo(this[j + 1]) > 0)
{
tmp = this[j + 1];
this[j + 1] = this[j];
this[j] = tmp;
}
}
}
} public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < m_count; i++)
yield return m_list[i];
} public IEnumerable<T> SortedEnumerator()
{
int[] index = new int[m_count];
for (int i = 0; i < m_count; i++)
index[i] = i;
int iTmp;
for (int i = m_count - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (m_list[index[j]].CompareTo(m_list[index[j + 1]]) > 0)
{
iTmp = index[j + 1];
index[j + 1] = index[j];
index[j] = iTmp;
}
}
}
for (int i = 0; i < m_count; i++)
yield return m_list[index[i]];
}
}
}错误 1 “P14_1.Assemble<T>”不会实现接口成员“System.Collections.IEnumerable.GetEnumerator()”。“P14_1.Assemble<T>.GetEnumerator()”或者是静态、非公共的,或者有错误的返回类型。 D:\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\Program.cs 29 18 ConsoleApplication2我是个C#半新手,今天我照着书上的这个程序编写了一段代码,也是这个错误,开始我以为是自己哪个程序中其它地方写错了,结果把书上的这段代码原封不动的运行也出现上面一样的错误,谁帮我看看是什么地方不对呀???????谢谢了
using System;
using System.Collections.Generic;namespace P14_1
{
public class AssembleIteratorSample
{
static void Main()
{
Assemble<int> iAsm = new Assemble<int>();
iAsm.Add(4);
iAsm.Add(12);
iAsm.Add(9);
iAsm.Add(1);
iAsm.Add(8);
Console.WriteLine("排序前:");
foreach (int i in iAsm)
Console.WriteLine(i);
Console.WriteLine("排序后:");
foreach (int i in iAsm.SortedEnumerator())
Console.WriteLine(i);
}
} /// <summary>
/// 泛型类: 集合Assemble
/// </summary>
public class Assemble<T> : IEnumerable<T>
where T : IComparable<T>
{
//字段
protected T[] m_list;
protected int m_count = 0;
protected int m_capacity = 100; //属性
public int Length
{
get
{
return m_count;
}
} public int Capacity
{
get
{
return m_list.Length;
}
} //索引函数
public T this[int index]
{
get
{
return m_list[index];
}
set
{
m_list[index] = value;
}
} //构造函数
public Assemble()
{
m_list = new T[m_capacity];
} public Assemble(int iCapacity)
{
m_capacity = iCapacity;
m_list = new T[m_capacity];
} //方法
public void Add(T tp)
{
if (m_count < m_list.Length)
m_list[m_count++] = tp;
} public void Remove(T tp)
{
int index = this.Find(tp);
if (index == -1)
return;
this.RemoveAt(index);
} public void RemoveAt(int index)
{
for (int i = index; i < m_count - 1; i++)
{
m_list[i] = m_list[i + 1];
}
m_list[--m_count] = default(T);
} public int Find(T tp)
{
for (int i = 0; i < m_count; i++)
if (m_list[i].Equals(tp))
return i;
return -1;
} public void Sort()
{
T tmp;
for (int i = m_count - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (this[j].CompareTo(this[j + 1]) > 0)
{
tmp = this[j + 1];
this[j + 1] = this[j];
this[j] = tmp;
}
}
}
} public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < m_count; i++)
yield return m_list[i];
} public IEnumerable<T> SortedEnumerator()
{
int[] index = new int[m_count];
for (int i = 0; i < m_count; i++)
index[i] = i;
int iTmp;
for (int i = m_count - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (m_list[index[j]].CompareTo(m_list[index[j + 1]]) > 0)
{
iTmp = index[j + 1];
index[j + 1] = index[j];
index[j] = iTmp;
}
}
}
for (int i = 0; i < m_count; i++)
yield return m_list[index[i]];
}
}
}错误 1 “P14_1.Assemble<T>”不会实现接口成员“System.Collections.IEnumerable.GetEnumerator()”。“P14_1.Assemble<T>.GetEnumerator()”或者是静态、非公共的,或者有错误的返回类型。 D:\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\Program.cs 29 18 ConsoleApplication2我是个C#半新手,今天我照着书上的这个程序编写了一段代码,也是这个错误,开始我以为是自己哪个程序中其它地方写错了,结果把书上的这段代码原封不动的运行也出现上面一样的错误,谁帮我看看是什么地方不对呀???????谢谢了
解决方案 »
- 过年了,问大家一个问题,有实际产品经验的肯定知道
- DataGridView中CheckBox列的值
- 采用FtpWebRequest访问远程FTP,如何删除文件,有权限
- [求助]关于MDI子窗体实例化
- 我的问题:此范围定义了“ convertString4 ”的局部变量。在线等!
- doc转bmp的思路
- =====50分求高手来看看这个问题,用.net实现横向图片分页
- 想学c#,请各位大虾推荐几本书啊
- 请问如何在控件调用父页面的方法(或属性)
- 100分求c#开发的软件中消息处理程序!比如生产部下生产通知单能通过软件通知相关的部门!
- Messbox.show的用法
- int space = t.IndexOf(' '); 这如何理解?单引号中为什么是空格?
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
是写书的人想当然犯的错误。
谢谢解答,我在后面添加了你说的那些代码,可是还是不行呀
我添加如下代码的话问题依旧噢~~IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}麻烦你再帮我看一下好吗???
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}
中的..return GetEnumerator();改成
return m_list.GetEnumerator(); 看看..不知道行不行
他提示
错误 1 使用泛型 类型“System.Collections.Generic.IEnumerator<T>”需要“1”个类型参数 D:\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\Program.cs 156 9 ConsoleApplication2错误地方就是IEnumerator IEnumerable.GetEnumerator()中的IEnumerator那里噢
我是在VS2005里面调试的
VS2005那里直接有个提示没看见……