自己编写的泛型集合类(其实是照着微软的List写的)


public class EntityCollectionsView<T>
{
public EntityCollectionsView(EntityCollections<T> collections)
{
this._collections = collections;
}
private EntityCollections<T> _collections;

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public T[] Items
{
get
{
T[] arr = new T[_collections.Count];
_collections.CopyTo(arr, 0);
return arr;
}
}
}

[Serializable, DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(EntityCollectionsView<>))]
public class EntityCollections<T> : ICollection<T>
{
public EntityCollections()
{
_array =new T[0];
}
private T[] _array;
private int _size;

/// <summary>
/// 索引器
/// </summary>
/// <param name="index">索引</param>
/// <returns></returns>
public T this[int index]
{
get
{
return _array[index];
}
set
{
_array[index] = value;
}
}

public int Capacity
{
get
{
return _array.Length;
}
set
{
if (value > 0)
{
T[] arr = new T[value];
if (_array != null)
{
Array.Copy(this._array,0,arr, 0,this._size);

}
_array = arr;
}
else
{
_array = new T[0];
}
}
}
public void Add(T item)
{
if (_array.Length == 0 || _array.Length == _size)
{
EnsureCapacity(_size + 1);
}
_array[_size++] = item;
}

 

private void EnsureCapacity(int min)
{
int num = _array.Length == 0 ? 4 : _array.Length * 2;
if (num < min)
{
num = min;
}
this.Capacity = num;

}

public void Clear()
{
this._array = new T[0];
this._size = 0;
this.Capacity = 0;
}

public bool Contains(T item)
{
if (item == null)
{
foreach (T val in _array)
{
if (val == null) return true;
}
}
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
foreach (var val in _array)
{
if (comparer.Equals(val, item))
{
return true;
}
}
return false;
}

public void CopyTo(T[] array, int arrayIndex)
{
if ((array != null) && (array.Rank != 1))
{
throw new EntityCollectionsException("要复制的数据为空或目标数组不是一维数组");
}
try
{
Array.Copy(this._array, 0, array, arrayIndex, this._size);
}
catch (Exception e)
{
throw e;
}
}

public int IndexOf(T item)
{
if (item == null) return -1;
return Array.IndexOf(_array, item);
}

public int Count
{
get
{
return _size;
}
}

public bool IsReadOnly
{
get { return false; }
}

public bool Remove(T item)
{
return this.RemoveAt(this.IndexOf(item));
}

public bool RemoveAt(int index)
{
if (index > _size||index<1) return false;
this._size--;
Array.Copy(_array, index + 1, _array, index, _size - index);
_array[this._size] = default(T);
return true;
}

public IEnumerator<T> GetEnumerator()
{
return new EntityEnumerator(this);
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}

public struct EntityEnumerator : IEnumerator<T>, IDisposable, System.Collections.IEnumerator
{
private int _index;
private T _current;
private EntityCollections<T> _lst;

internal EntityEnumerator(EntityCollections<T> list)
{
this._lst = list;
this._index = 0;
this._current = default(T);
}
public T Current
{
get
{
return _current;
}
}

public void Dispose()
{

}

object System.Collections.IEnumerator.Current
{
get
{
return this.Current;
}
}

public bool MoveNext()
{
if (_index < _lst.Count)
{
_current = _lst[_index];
_index++;
return true;
}
return false;
}

public void Reset()
{
_index = 0;
_current = default(T);
}

}


public class EntityCollectionsException : Exception
{
public EntityCollectionsException()
: base()
{

}

public EntityCollectionsException(string message)
: base(message)
{

}

protected EntityCollectionsException(SerializationInfo info, StreamingContext context)
: base(info, context)
{

}
public EntityCollectionsException(string message, Exception inner)
: base(message, inner)
{

}
}
}

posted @ 2013-04-08 11:35  看看我的眼神  阅读(306)  评论(0编辑  收藏  举报