实现ICollection的 CopyTo,自定义实体泛型类问题
//自定义泛型集合 IList<T>--->[ICollection<T>, IEnumerable<T>, IEnumerable] , IList
public class EMPPlayers<T> : IList<T>, ICollection where T : PlayerObject
{
private readonly static string pGUID
= DateTime.UtcNow.Ticks.ToString().Trim('-') + "-" + Guid.NewGuid().ToString();
private static volatile EMPPlayers<T> instane = null;
private List<T> mlist = null;
protected EMPPlayers()
{
mlist = new List<T>();
}
public static EMPPlayers<T> GetPlayerInstane()
{
System.Threading.Mutex mutex = new System.Threading.Mutex();
mutex.WaitOne();
if (instane == null) //双检查
{
lock (pGUID)
{
if (instane == null)
{
instane = new EMPPlayers<T>();
}
//else {
//}
}
}
mutex.Close();
return instane;
}
#region IList<T> 成员
public int IndexOf(T item)
{
return mlist.IndexOf(item);
}
public void Insert(int index, T item)
{
mlist.Insert(index, item);
}
public void RemoveAt(int index)
{
mlist.RemoveAt(index);
}
public T this[int index]
{
get
{
if (index >= 0 && mlist.Count > index)
return mlist[index];
return default(T);
}
set
{
mlist[index] = value;
}
}
#endregion
#region ICollection<T> 成员
public void Add(T item)
{
mlist.Add(item);
}
public void Clear()
{
mlist.Clear();
}
public bool Contains(T item)
{
return mlist.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
mlist.CopyTo(array, arrayIndex);
}
public int Count
{
get { return mlist.Count; }
}
public bool IsReadOnly
{
get
{
return false;
}
}
public bool Remove(T item)
{
return mlist.Remove(item);
}
#endregion
#region IEnumerable<T> 成员
public IEnumerator<T> GetEnumerator()
{
//return mlist.ToArray();
foreach (T obj in mlist)
{
yield return obj;
}
}
#endregion
#region IEnumerable 成员
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
//System.Collections.ArrayList enumObj = new System.Collections.ArrayList();
//foreach (T item in mlist)
//{
// enumObj.Add(T);
//}
//enumObj.GetEnumerator();
return GetEnumerator();
}
#endregion
//#region IList 成员
//public int Add(object value)
//{
// throw new Exception("The method or operation is not implemented.");
//}
//public bool Contains(object value)
//{
// throw new Exception("The method or operation is not implemented.");
//}
//public int IndexOf(object value)
//{
// throw new Exception("The method or operation is not implemented.");
//}
//public void Insert(int index, object value)
//{
// throw new Exception("The method or operation is not implemented.");
//}
//public bool IsFixedSize
//{
// get { throw new Exception("The method or operation is not implemented."); }
//}
//public void Remove(object value)
//{
// throw new Exception("The method or operation is not implemented.");
//}
//object IList.this[int index]
//{
// get
// {
// throw new Exception("The method or operation is not implemented.");
// }
// set
// {
// throw new Exception("The method or operation is not implemented.");
// }
//}
//#endregion
#region ICollection 成员
public void CopyTo(Array array, int index)
{
//T[] aT = new T[] { (T)array.Clone() };
//T[] xT = array.Clone() as T[];
//if (xT == null) {
// throw new ArgumentNullException();
//}
//CopyTo(xT, 0);
//mlist.CopyTo(array, 0);
//array.CopyTo(mlist.ToArray(), 0);
Array.Copy(mlist.ToArray(), array, 0);
}
public bool IsSynchronized
{
get { return mlist.ToArray().IsSynchronized; }
}
public object SyncRoot
{
get { return mlist.ToArray ().SyncRoot; }
}
#endregion
public void AddRange(IEnumerable<T> collection)
{
mlist.AddRange(collection);
}
public int LastIndexOf(T item) {
return mlist.LastIndexOf(item);
}
}
...全文
相关推荐
ICollection实现
同时,它还可以作为其他自定义集合类的基础,通过继承并添加额外的方法和属性来扩展其功能。
学习C#中ICollection接口的使用方法
在这个示例中,我们首先创建了一个MyCollection的实例,并分别使用Add()方法向集合中添加了三个整数。然后,我们使用Count属性获取集合中元素的数量,并使用Contains()方法检查集合是否包含特定元素。接下来,我们使用CopyTo()方法将集合的元素复制到一个整型数组中,并使用foreach循环打印出数组中的元素。最后,我们使用Remove()方法删除集合中的一个元素,并再次使用Count属性来确认元素是否被成功删除。在本文中,我们将探讨C#中ICollection接口的使用方法。
做一个集合类,并按索引二分法查找,继承IComparer、IComparable、ICollection
在.NET框架中,`IComparer`、`IComparable`和`ICollection`接口是面向对象编程中常见的接口,用于实现自定义排序、比较和集合管理。
C程序设计资料全集PPT课件.pptx
**Array类**作为所有数组的基类,实现了IList、ICollection和IEnumerable接口,还实现了ICloneable接口。
C#常用类简介
Array类实现了三个重要的接口:IEnumerable、ICollection和IList。这些接口让数组具备了遍历、计数和索引访问的能力:1.


