List泛型
.Net自从2.0以后开始支持泛型。
泛型的作用:可以创建独立于被包含类型的类和方法。泛型类使用泛型类型,并可以根据需要使用特定的类型替换泛型类型。这就保证了类型安全性:如果某个类型不支持泛型类,编译器就会出现错误。
不多说,先记录一下源码:
- // ==++==
- //
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //
- // ==--==
- /*============================================================
- **
- ** Class: List
- **
- ** <OWNER>Microsoft</OWNER>
- **
- ** Purpose: Implements a generic, dynamically sized list as an
- ** array.
- **
- **
- ===========================================================*/
- namespace System.Collections.Generic {
- using System;
- using System.Runtime;
- using System.Runtime.Versioning;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
- using System.Collections.ObjectModel;
- using System.Security.Permissions;
- // Implements a variable-size List that uses an array of objects to store the
- // elements. A List has a capacity, which is the allocated length
- // of the internal array. As elements are added to a List, the capacity
- // of the List is automatically increased as required by reallocating the
- // internal array.
- //
- [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
- [DebuggerDisplay("Count = {Count}")]
- [Serializable]
- public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
- {
- private const int _defaultCapacity = ;
- private T[] _items;
- [ContractPublicPropertyName("Count")]
- private int _size;
- private int _version;
- [NonSerialized]
- private Object _syncRoot;
- static readonly T[] _emptyArray = new T[];
- // Constructs a List. The list is initially empty and has a capacity
- // of zero. Upon adding the first element to the list the capacity is
- // increased to 16, and then increased in multiples of two as required.
- public List() {
- _items = _emptyArray;
- }
- // Constructs a List with a given initial capacity. The list is
- // initially empty, but will have room for the given number of elements
- // before any reallocations are required.
- //
- public List(int capacity) {
- if (capacity < ) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- Contract.EndContractBlock();
- if (capacity == )
- _items = _emptyArray;
- else
- _items = new T[capacity];
- }
- // Constructs a List, copying the contents of the given collection. The
- // size and capacity of the new list will both be equal to the size of the
- // given collection.
- //
- public List(IEnumerable<T> collection) {
- if (collection==null)
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
- Contract.EndContractBlock();
- ICollection<T> c = collection as ICollection<T>;
- if( c != null) {
- int count = c.Count;
- if (count == )
- {
- _items = _emptyArray;
- }
- else {
- _items = new T[count];
- c.CopyTo(_items, );
- _size = count;
- }
- }
- else {
- _size = ;
- _items = _emptyArray;
- // This enumerable could be empty. Let Add allocate a new array, if needed.
- // Note it will also go to _defaultCapacity first, not 1, then 2, etc.
- using(IEnumerator<T> en = collection.GetEnumerator()) {
- while(en.MoveNext()) {
- Add(en.Current);
- }
- }
- }
- }
- // Gets and sets the capacity of this list. The capacity is the size of
- // the internal array used to hold items. When set, the internal
- // array of the list is reallocated to the given capacity.
- //
- public int Capacity {
- get {
- Contract.Ensures(Contract.Result<int>() >= );
- return _items.Length;
- }
- set {
- if (value < _size) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
- }
- Contract.EndContractBlock();
- if (value != _items.Length) {
- if (value > ) {
- T[] newItems = new T[value];
- if (_size > ) {
- Array.Copy(_items, , newItems, , _size);
- }
- _items = newItems;
- }
- else {
- _items = _emptyArray;
- }
- }
- }
- }
- // Read-only property describing how many elements are in the List.
- public int Count {
- get {
- Contract.Ensures(Contract.Result<int>() >= );
- return _size;
- }
- }
- bool System.Collections.IList.IsFixedSize {
- get { return false; }
- }
- // Is this List read-only?
- bool ICollection<T>.IsReadOnly {
- get { return false; }
- }
- bool System.Collections.IList.IsReadOnly {
- get { return false; }
- }
- // Is this List synchronized (thread-safe)?
- bool System.Collections.ICollection.IsSynchronized {
- get { return false; }
- }
- // Synchronization root for this object.
- Object System.Collections.ICollection.SyncRoot {
- get {
- if( _syncRoot == null) {
- System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
- }
- return _syncRoot;
- }
- }
- // Sets or Gets the element at the given index.
- //
- public T this[int index] {
- get {
- // Following trick can reduce the range check by one
- if ((uint) index >= (uint)_size) {
- ThrowHelper.ThrowArgumentOutOfRangeException();
- }
- Contract.EndContractBlock();
- return _items[index];
- }
- set {
- if ((uint) index >= (uint)_size) {
- ThrowHelper.ThrowArgumentOutOfRangeException();
- }
- Contract.EndContractBlock();
- _items[index] = value;
- _version++;
- }
- }
- private static bool IsCompatibleObject(object value) {
- // Non-null values are fine. Only accept nulls if T is a class or Nullable<U>.
- // Note that default(T) is not equal to null for value types except when T is Nullable<U>.
- return ((value is T) || (value == null && default(T) == null));
- }
- Object System.Collections.IList.this[int index] {
- get {
- return this[index];
- }
- set {
- ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
- try {
- this[index] = (T)value;
- }
- catch (InvalidCastException) {
- ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
- }
- }
- }
- // Adds the given object to the end of this list. The size of the list is
- // increased by one. If required, the capacity of the list is doubled
- // before adding the new element.
- //
- public void Add(T item) {
- if (_size == _items.Length) EnsureCapacity(_size + );
- _items[_size++] = item;
- _version++;
- }
- int System.Collections.IList.Add(Object item)
- {
- ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
- try {
- Add((T) item);
- }
- catch (InvalidCastException) {
- ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
- }
- return Count - ;
- }
- // Adds the elements of the given collection to the end of this list. If
- // required, the capacity of the list is increased to twice the previous
- // capacity or the new size, whichever is larger.
- //
- public void AddRange(IEnumerable<T> collection) {
- Contract.Ensures(Count >= Contract.OldValue(Count));
- InsertRange(_size, collection);
- }
- public ReadOnlyCollection<T> AsReadOnly() {
- Contract.Ensures(Contract.Result<ReadOnlyCollection<T>>() != null);
- return new ReadOnlyCollection<T>(this);
- }
- // Searches a section of the list for a given element using a binary search
- // algorithm. Elements of the list are compared to the search value using
- // the given IComparer interface. If comparer is null, elements of
- // the list are compared to the search value using the IComparable
- // interface, which in that case must be implemented by all elements of the
- // list and the given search value. This method assumes that the given
- // section of the list is already sorted; if this is not the case, the
- // result will be incorrect.
- //
- // The method returns the index of the given value in the list. If the
- // list does not contain the given value, the method returns a negative
- // integer. The bitwise complement operator (~) can be applied to a
- // negative result to produce the index of the first element (if any) that
- // is larger than the given search value. This is also the index at which
- // the search value should be inserted into the list in order for the list
- // to remain sorted.
- //
- // The method uses the Array.BinarySearch method to perform the
- // search.
- //
- public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
- if (index < )
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- if (count < )
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- if (_size - index < count)
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
- Contract.Ensures(Contract.Result<int>() <= index + count);
- Contract.EndContractBlock();
- return Array.BinarySearch<T>(_items, index, count, item, comparer);
- }
- public int BinarySearch(T item)
- {
- Contract.Ensures(Contract.Result<int>() <= Count);
- return BinarySearch(, Count, item, null);
- }
- public int BinarySearch(T item, IComparer<T> comparer)
- {
- Contract.Ensures(Contract.Result<int>() <= Count);
- return BinarySearch(, Count, item, comparer);
- }
- // Clears the contents of List.
- public void Clear() {
- if (_size > )
- {
- Array.Clear(_items, , _size); // Don't need to doc this but we clear the elements so that the gc can reclaim the references.
- _size = ;
- }
- _version++;
- }
- // Contains returns true if the specified element is in the List.
- // It does a linear, O(n) search. Equality is determined by calling
- // item.Equals().
- //
- public bool Contains(T item) {
- if ((Object) item == null) {
- for(int i=; i<_size; i++)
- if ((Object) _items[i] == null)
- return true;
- return false;
- }
- else {
- EqualityComparer<T> c = EqualityComparer<T>.Default;
- for(int i=; i<_size; i++) {
- if (c.Equals(_items[i], item)) return true;
- }
- return false;
- }
- }
- bool System.Collections.IList.Contains(Object item)
- {
- if(IsCompatibleObject(item)) {
- return Contains((T) item);
- }
- return false;
- }
- public List<TOutput> ConvertAll<TOutput>(Converter<T,TOutput> converter) {
- if( converter == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
- }
- // @
- Contract.EndContractBlock();
- List<TOutput> list = new List<TOutput>(_size);
- for( int i = ; i< _size; i++) {
- list._items[i] = converter(_items[i]);
- }
- list._size = _size;
- return list;
- }
- // Copies this List into array, which must be of a
- // compatible array type.
- //
- public void CopyTo(T[] array) {
- CopyTo(array, );
- }
- // Copies this List into array, which must be of a
- // compatible array type.
- //
- void System.Collections.ICollection.CopyTo(Array array, int arrayIndex) {
- if ((array != null) && (array.Rank != )) {
- ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
- }
- Contract.EndContractBlock();
- try {
- // Array.Copy will check for NULL.
- Array.Copy(_items, , array, arrayIndex, _size);
- }
- catch(ArrayTypeMismatchException){
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
- }
- }
- // Copies a section of this list to the given array at the given index.
- //
- // The method uses the Array.Copy method to copy the elements.
- //
- public void CopyTo(int index, T[] array, int arrayIndex, int count) {
- if (_size - index < count) {
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
- }
- Contract.EndContractBlock();
- // Delegate rest of error checking to Array.Copy.
- Array.Copy(_items, index, array, arrayIndex, count);
- }
- public void CopyTo(T[] array, int arrayIndex) {
- // Delegate rest of error checking to Array.Copy.
- Array.Copy(_items, , array, arrayIndex, _size);
- }
- // Ensures that the capacity of this list is at least the given minimum
- // value. If the currect capacity of the list is less than min, the
- // capacity is increased to twice the current capacity or to min,
- // whichever is larger.
- private void EnsureCapacity(int min) {
- if (_items.Length < min) {
- int newCapacity = _items.Length == ? _defaultCapacity : _items.Length * ;
- // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
- // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
- if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
- if (newCapacity < min) newCapacity = min;
- Capacity = newCapacity;
- }
- }
- public bool Exists(Predicate<T> match) {
- return FindIndex(match) != -;
- }
- public T Find(Predicate<T> match) {
- if( match == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.EndContractBlock();
- for(int i = ; i < _size; i++) {
- if(match(_items[i])) {
- return _items[i];
- }
- }
- return default(T);
- }
- public List<T> FindAll(Predicate<T> match) {
- if( match == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.EndContractBlock();
- List<T> list = new List<T>();
- for(int i = ; i < _size; i++) {
- if(match(_items[i])) {
- list.Add(_items[i]);
- }
- }
- return list;
- }
- public int FindIndex(Predicate<T> match) {
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() < Count);
- return FindIndex(, _size, match);
- }
- public int FindIndex(int startIndex, Predicate<T> match) {
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() < startIndex + Count);
- return FindIndex(startIndex, _size - startIndex, match);
- }
- public int FindIndex(int startIndex, int count, Predicate<T> match) {
- if( (uint)startIndex > (uint)_size ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
- }
- if (count < || startIndex > _size - count) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
- }
- if( match == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() < startIndex + count);
- Contract.EndContractBlock();
- int endIndex = startIndex + count;
- for( int i = startIndex; i < endIndex; i++) {
- if( match(_items[i])) return i;
- }
- return -;
- }
- public T FindLast(Predicate<T> match) {
- if( match == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.EndContractBlock();
- for(int i = _size - ; i >= ; i--) {
- if(match(_items[i])) {
- return _items[i];
- }
- }
- return default(T);
- }
- public int FindLastIndex(Predicate<T> match) {
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() < Count);
- return FindLastIndex(_size - , _size, match);
- }
- public int FindLastIndex(int startIndex, Predicate<T> match) {
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() <= startIndex);
- return FindLastIndex(startIndex, startIndex + , match);
- }
- public int FindLastIndex(int startIndex, int count, Predicate<T> match) {
- if( match == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() <= startIndex);
- Contract.EndContractBlock();
- if(_size == ) {
- // Special case for 0 length List
- if( startIndex != -) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
- }
- }
- else {
- // Make sure we're not out of range
- if ( (uint)startIndex >= (uint)_size) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
- }
- }
- // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
- if (count < || startIndex - count + < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
- }
- int endIndex = startIndex - count;
- for( int i = startIndex; i > endIndex; i--) {
- if( match(_items[i])) {
- return i;
- }
- }
- return -;
- }
- public void ForEach(Action<T> action) {
- if( action == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.EndContractBlock();
- int version = _version;
- for(int i = ; i < _size; i++) {
- if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5) {
- break;
- }
- action(_items[i]);
- }
- if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
- ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
- }
- // Returns an enumerator for this list with the given
- // permission for removal of elements. If modifications made to the list
- // while an enumeration is in progress, the MoveNext and
- // GetObject methods of the enumerator will throw an exception.
- //
- public Enumerator GetEnumerator() {
- return new Enumerator(this);
- }
- /// <internalonly/>
- IEnumerator<T> IEnumerable<T>.GetEnumerator() {
- return new Enumerator(this);
- }
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
- return new Enumerator(this);
- }
- public List<T> GetRange(int index, int count) {
- if (index < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (count < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (_size - index < count) {
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
- }
- Contract.Ensures(Contract.Result<List<T>>() != null);
- Contract.EndContractBlock();
- List<T> list = new List<T>(count);
- Array.Copy(_items, index, list._items, , count);
- list._size = count;
- return list;
- }
- // Returns the index of the first occurrence of a given value in a range of
- // this list. The list is searched forwards from beginning to end.
- // The elements of the list are compared to the given value using the
- // Object.Equals method.
- //
- // This method uses the Array.IndexOf method to perform the
- // search.
- //
- public int IndexOf(T item) {
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() < Count);
- return Array.IndexOf(_items, item, , _size);
- }
- int System.Collections.IList.IndexOf(Object item)
- {
- if(IsCompatibleObject(item)) {
- return IndexOf((T)item);
- }
- return -;
- }
- // Returns the index of the first occurrence of a given value in a range of
- // this list. The list is searched forwards, starting at index
- // index and ending at count number of elements. The
- // elements of the list are compared to the given value using the
- // Object.Equals method.
- //
- // This method uses the Array.IndexOf method to perform the
- // search.
- //
- public int IndexOf(T item, int index) {
- if (index > _size)
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() < Count);
- Contract.EndContractBlock();
- return Array.IndexOf(_items, item, index, _size - index);
- }
- // Returns the index of the first occurrence of a given value in a range of
- // this list. The list is searched forwards, starting at index
- // index and upto count number of elements. The
- // elements of the list are compared to the given value using the
- // Object.Equals method.
- //
- // This method uses the Array.IndexOf method to perform the
- // search.
- //
- public int IndexOf(T item, int index, int count) {
- if (index > _size)
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
- if (count < || index > _size - count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() < Count);
- Contract.EndContractBlock();
- return Array.IndexOf(_items, item, index, count);
- }
- // Inserts an element into this list at a given index. The size of the list
- // is increased by one. If required, the capacity of the list is doubled
- // before inserting the new element.
- //
- public void Insert(int index, T item) {
- // Note that insertions at the end are legal.
- if ((uint) index > (uint)_size) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
- }
- Contract.EndContractBlock();
- if (_size == _items.Length) EnsureCapacity(_size + );
- if (index < _size) {
- Array.Copy(_items, index, _items, index + , _size - index);
- }
- _items[index] = item;
- _size++;
- _version++;
- }
- void System.Collections.IList.Insert(int index, Object item)
- {
- ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
- try {
- Insert(index, (T) item);
- }
- catch (InvalidCastException) {
- ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
- }
- }
- // Inserts the elements of the given collection at a given index. If
- // required, the capacity of the list is increased to twice the previous
- // capacity or the new size, whichever is larger. Ranges may be added
- // to the end of the list by setting index to the List's size.
- //
- public void InsertRange(int index, IEnumerable<T> collection) {
- if (collection==null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
- }
- if ((uint)index > (uint)_size) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
- }
- Contract.EndContractBlock();
- ICollection<T> c = collection as ICollection<T>;
- if( c != null ) { // if collection is ICollection<T>
- int count = c.Count;
- if (count > ) {
- EnsureCapacity(_size + count);
- if (index < _size) {
- Array.Copy(_items, index, _items, index + count, _size - index);
- }
- // If we're inserting a List into itself, we want to be able to deal with that.
- if (this == c) {
- // Copy first part of _items to insert location
- Array.Copy(_items, , _items, index, index);
- // Copy last part of _items back to inserted location
- Array.Copy(_items, index+count, _items, index*, _size-index);
- }
- else {
- T[] itemsToInsert = new T[count];
- c.CopyTo(itemsToInsert, );
- itemsToInsert.CopyTo(_items, index);
- }
- _size += count;
- }
- }
- else {
- using(IEnumerator<T> en = collection.GetEnumerator()) {
- while(en.MoveNext()) {
- Insert(index++, en.Current);
- }
- }
- }
- _version++;
- }
- // Returns the index of the last occurrence of a given value in a range of
- // this list. The list is searched backwards, starting at the end
- // and ending at the first element in the list. The elements of the list
- // are compared to the given value using the Object.Equals method.
- //
- // This method uses the Array.LastIndexOf method to perform the
- // search.
- //
- public int LastIndexOf(T item)
- {
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(Contract.Result<int>() < Count);
- if (_size == ) { // Special case for empty list
- return -;
- }
- else {
- return LastIndexOf(item, _size - , _size);
- }
- }
- // Returns the index of the last occurrence of a given value in a range of
- // this list. The list is searched backwards, starting at index
- // index and ending at the first element in the list. The
- // elements of the list are compared to the given value using the
- // Object.Equals method.
- //
- // This method uses the Array.LastIndexOf method to perform the
- // search.
- //
- public int LastIndexOf(T item, int index)
- {
- if (index >= _size)
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(((Count == ) && (Contract.Result<int>() == -)) || ((Count > ) && (Contract.Result<int>() <= index)));
- Contract.EndContractBlock();
- return LastIndexOf(item, index, index + );
- }
- // Returns the index of the last occurrence of a given value in a range of
- // this list. The list is searched backwards, starting at index
- // index and upto count elements. The elements of
- // the list are compared to the given value using the Object.Equals
- // method.
- //
- // This method uses the Array.LastIndexOf method to perform the
- // search.
- //
- public int LastIndexOf(T item, int index, int count) {
- if ((Count != ) && (index < )) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if ((Count !=) && (count < )) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- Contract.Ensures(Contract.Result<int>() >= -);
- Contract.Ensures(((Count == ) && (Contract.Result<int>() == -)) || ((Count > ) && (Contract.Result<int>() <= index)));
- Contract.EndContractBlock();
- if (_size == ) { // Special case for empty list
- return -;
- }
- if (index >= _size) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
- }
- if (count > index + ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
- }
- return Array.LastIndexOf(_items, item, index, count);
- }
- // Removes the element at the given index. The size of the list is
- // decreased by one.
- //
- public bool Remove(T item) {
- int index = IndexOf(item);
- if (index >= ) {
- RemoveAt(index);
- return true;
- }
- return false;
- }
- void System.Collections.IList.Remove(Object item)
- {
- if(IsCompatibleObject(item)) {
- Remove((T) item);
- }
- }
- // This method removes all items which matches the predicate.
- // The complexity is O(n).
- public int RemoveAll(Predicate<T> match) {
- if( match == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.Ensures(Contract.Result<int>() >= );
- Contract.Ensures(Contract.Result<int>() <= Contract.OldValue(Count));
- Contract.EndContractBlock();
- int freeIndex = ; // the first free slot in items array
- // Find the first item which needs to be removed.
- while( freeIndex < _size && !match(_items[freeIndex])) freeIndex++;
- if( freeIndex >= _size) return ;
- int current = freeIndex + ;
- while( current < _size) {
- // Find the first item which needs to be kept.
- while( current < _size && match(_items[current])) current++;
- if( current < _size) {
- // copy item to the free slot.
- _items[freeIndex++] = _items[current++];
- }
- }
- Array.Clear(_items, freeIndex, _size - freeIndex);
- int result = _size - freeIndex;
- _size = freeIndex;
- _version++;
- return result;
- }
- // Removes the element at the given index. The size of the list is
- // decreased by one.
- //
- public void RemoveAt(int index) {
- if ((uint)index >= (uint)_size) {
- ThrowHelper.ThrowArgumentOutOfRangeException();
- }
- Contract.EndContractBlock();
- _size--;
- if (index < _size) {
- Array.Copy(_items, index + , _items, index, _size - index);
- }
- _items[_size] = default(T);
- _version++;
- }
- // Removes a range of elements from this list.
- //
- public void RemoveRange(int index, int count) {
- if (index < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (count < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (_size - index < count)
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
- Contract.EndContractBlock();
- if (count > ) {
- int i = _size;
- _size -= count;
- if (index < _size) {
- Array.Copy(_items, index + count, _items, index, _size - index);
- }
- Array.Clear(_items, _size, count);
- _version++;
- }
- }
- // Reverses the elements in this list.
- public void Reverse() {
- Reverse(, Count);
- }
- // Reverses the elements in a range of this list. Following a call to this
- // method, an element in the range given by index and count
- // which was previously located at index i will now be located at
- // index index + (index + count - i - 1).
- //
- // This method uses the Array.Reverse method to reverse the
- // elements.
- //
- public void Reverse(int index, int count) {
- if (index < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (count < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (_size - index < count)
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
- Contract.EndContractBlock();
- Array.Reverse(_items, index, count);
- _version++;
- }
- // Sorts the elements in this list. Uses the default comparer and
- // Array.Sort.
- public void Sort()
- {
- Sort(, Count, null);
- }
- // Sorts the elements in this list. Uses Array.Sort with the
- // provided comparer.
- public void Sort(IComparer<T> comparer)
- {
- Sort(, Count, comparer);
- }
- // Sorts the elements in a section of this list. The sort compares the
- // elements to each other using the given IComparer interface. If
- // comparer is null, the elements are compared to each other using
- // the IComparable interface, which in that case must be implemented by all
- // elements of the list.
- //
- // This method uses the Array.Sort method to sort the elements.
- //
- public void Sort(int index, int count, IComparer<T> comparer) {
- if (index < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (count < ) {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (_size - index < count)
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
- Contract.EndContractBlock();
- Array.Sort<T>(_items, index, count, comparer);
- _version++;
- }
- public void Sort(Comparison<T> comparison) {
- if( comparison == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.EndContractBlock();
- if( _size > ) {
- IComparer<T> comparer = new Array.FunctorComparer<T>(comparison);
- Array.Sort(_items, , _size, comparer);
- }
- }
- // ToArray returns a new Object array containing the contents of the List.
- // This requires copying the List, which is an O(n) operation.
- public T[] ToArray() {
- Contract.Ensures(Contract.Result<T[]>() != null);
- Contract.Ensures(Contract.Result<T[]>().Length == Count);
- T[] array = new T[_size];
- Array.Copy(_items, , array, , _size);
- return array;
- }
- // Sets the capacity of this list to the size of the list. This method can
- // be used to minimize a list's memory overhead once it is known that no
- // new elements will be added to the list. To completely clear a list and
- // release all memory referenced by the list, execute the following
- // statements:
- //
- // list.Clear();
- // list.TrimExcess();
- //
- public void TrimExcess() {
- int threshold = (int)(((double)_items.Length) * 0.9);
- if( _size < threshold ) {
- Capacity = _size;
- }
- }
- public bool TrueForAll(Predicate<T> match) {
- if( match == null) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
- }
- Contract.EndContractBlock();
- for(int i = ; i < _size; i++) {
- if( !match(_items[i])) {
- return false;
- }
- }
- return true;
- }
- internal static IList<T> Synchronized(List<T> list) {
- return new SynchronizedList(list);
- }
- [Serializable()]
- internal class SynchronizedList : IList<T> {
- private List<T> _list;
- private Object _root;
- internal SynchronizedList(List<T> list) {
- _list = list;
- _root = ((System.Collections.ICollection)list).SyncRoot;
- }
- public int Count {
- get {
- lock (_root) {
- return _list.Count;
- }
- }
- }
- public bool IsReadOnly {
- get {
- return ((ICollection<T>)_list).IsReadOnly;
- }
- }
- public void Add(T item) {
- lock (_root) {
- _list.Add(item);
- }
- }
- public void Clear() {
- lock (_root) {
- _list.Clear();
- }
- }
- public bool Contains(T item) {
- lock (_root) {
- return _list.Contains(item);
- }
- }
- public void CopyTo(T[] array, int arrayIndex) {
- lock (_root) {
- _list.CopyTo(array, arrayIndex);
- }
- }
- public bool Remove(T item) {
- lock (_root) {
- return _list.Remove(item);
- }
- }
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
- lock (_root) {
- return _list.GetEnumerator();
- }
- }
- IEnumerator<T> IEnumerable<T>.GetEnumerator() {
- lock (_root) {
- return ((IEnumerable<T>)_list).GetEnumerator();
- }
- }
- public T this[int index] {
- get {
- lock(_root) {
- return _list[index];
- }
- }
- set {
- lock(_root) {
- _list[index] = value;
- }
- }
- }
- public int IndexOf(T item) {
- lock (_root) {
- return _list.IndexOf(item);
- }
- }
- public void Insert(int index, T item) {
- lock (_root) {
- _list.Insert(index, item);
- }
- }
- public void RemoveAt(int index) {
- lock (_root) {
- _list.RemoveAt(index);
- }
- }
- }
- [Serializable]
- public struct Enumerator : IEnumerator<T>, System.Collections.IEnumerator
- {
- private List<T> list;
- private int index;
- private int version;
- private T current;
- internal Enumerator(List<T> list) {
- this.list = list;
- index = ;
- version = list._version;
- current = default(T);
- }
- public void Dispose() {
- }
- public bool MoveNext() {
- List<T> localList = list;
- if (version == localList._version && ((uint)index < (uint)localList._size))
- {
- current = localList._items[index];
- index++;
- return true;
- }
- return MoveNextRare();
- }
- private bool MoveNextRare()
- {
- if (version != list._version) {
- ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
- }
- index = list._size + ;
- current = default(T);
- return false;
- }
- public T Current {
- get {
- return current;
- }
- }
- Object System.Collections.IEnumerator.Current {
- get {
- if( index == || index == list._size + ) {
- ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
- }
- return Current;
- }
- }
- void System.Collections.IEnumerator.Reset() {
- if (version != list._version) {
- ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
- }
- index = ;
- current = default(T);
- }
- }
- }
- }
创建一个Account类
public class Account
{
public string Name { get; private set; }
public decimal Balance { get; private set; } public Account(string name, Decimal balance)
{
this.Name = name;
this.Balance = balance;
}
}
创建一个Account类的集合LIst:
var account = new List<Account>()
{
new Account("Christian",),
new Account("Stephanie",),
new Account("Angela",),
new Account("Matthias",)
};
跟踪代码可见:
1、通过List类的构造函数创建一个模板类型T(Account)的数组:
static readonly T[] _emptyArray = new T[]; // Constructs a List. The list is initially empty and has a capacity
// of zero. Upon adding the first element to the list the capacity is
// increased to 16, and then increased in multiples of two as required.
public List() {
_items = _emptyArray;
}
2、创建Account 对象(略)
3、调用List类的Add方法。
public void Add(T item) {
if (_size == _items.Length) EnsureCapacity(_size + );
_items[_size++] = item;
_version++;
}
1 // 确保该列表的容量至少是给定的最小值。如果列表的currect容量小于min,容量将增加到当前容量的两倍或最小,无论哪个更大。
5 private void EnsureCapacity(int min) {
6 if (_items.Length < min) {
7 int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;//_defaultCapacity =4
8 // 允许列表在遇到溢出之前增加到最大容量(~ 2G元素)。
9 // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
10 if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;//Arrat,NaxArrayLength = 2146435071
11 if (newCapacity < min) newCapacity = min;
12 Capacity = newCapacity;//=>
13 }
14 }
public int Capacity {
get {
Contract.Ensures(Contract.Result<int>() >= );
return _items.Length;
}
set {
if (value < _size) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
}
Contract.EndContractBlock(); if (value != _items.Length) {
if (value > ) {
T[] newItems = new T[value];
if (_size > ) {
Array.Copy(_items, , newItems, , _size);
}
_items = newItems;
}
else {
_items = _emptyArray;
}
}
}
}
未多解释,直接上的代码,通过上面的代码可以看出其实List的实现,是同过T[]数组实现的。
还未深掘泛型的实现。研究中。。。
List泛型的更多相关文章
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
- .NET面试题系列[8] - 泛型
“可变性是以一种类型安全的方式,将一个对象作为另一个对象来使用.“ - Jon Skeet .NET面试题系列目录 .NET面试题系列[1] - .NET框架基础知识(1) .NET面试题系列[2] ...
- C#4.0泛型的协变,逆变深入剖析
C#4.0中有一个新特性:协变与逆变.可能很多人在开发过程中不常用到,但是深入的了解他们,肯定是有好处的. 协变和逆变体现在泛型的接口和委托上面,也就是对泛型参数的声明,可以声明为协变,或者逆变.什么 ...
- 编写高质量代码:改善Java程序的151个建议(第7章:泛型和反射___建议106~109)
建议106:动态代理可以使代理模式更加灵活 Java的反射框架提供了动态代理(Dynamic Proxy)机制,允许在运行期对目标类生成代理,避免重复开发.我们知道一个静态代理是通过主题角色(Prox ...
- 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- C#泛型详解(转)
初步理解泛型: http://www.cnblogs.com/wilber2013/p/4291435.html 泛型中的类型约束和类型推断 http://www.cnblogs.com/wilber ...
- C# 泛型
C# 泛型 1.定义泛型类 在类定义中包含尖括号语法,即可创建泛型类: class MyGenericClass<T> { //Add code } 其中T可以遵循C#命名规则的任意字符. ...
- java8中lambda表达式的应用,以及一些泛型相关
语法部分就不写了,我们直接抛出一个实际问题,看看java8的这些新特性究竟能给我们带来哪些便利 顺带用到一些泛型编程,一切都是为了简化代码 场景: 一个数据类,用于记录职工信息 public clas ...
- java 泛型
1.Student stu =tool.getObj();右边得到的是Object类型,需要向下转型,强转换. 2. 3. 4.泛型方法不能被静态修饰这样写 5.如果想定义定义静态泛型方法,只能这样写 ...
- Java泛型的历史
为什么Java泛型会有当前的缺陷? 之前的章节里已经说明了Java泛型擦除会导致的问题,C++和C#的泛型都是在运行时存在的,难道Java天然不支持“真正的泛型”吗? 事实上,在Java1.5在200 ...
随机推荐
- 分布式缓存Redis的持久化方式RDB和AOF
一.前言 Redis支持两种方式的持久化,RDB和AOF.RDB会根据指定的规则“定时”将内存中的数据存储到硬盘上,AOF会在每次执行命令后将命令本身记录下来.两种持久化方式可以单独使用其中一种,但更 ...
- 矩阵matrix变换的用法(css3属性transform: matrix)
参数 2D矩阵的表示 matrix(a,b,c,d,e,f),其中6个参数在矩阵的分布: -- -- | a c e | | b d f | | 0 0 1 | -- -- 在CSS3中矩阵的原始值是 ...
- Windows 64 位 mysql 5.7以上版本包解压中没有data目录和my-default.ini和my.ini文件以及服务无法启动的解决办法以及修改初始密码的方法
下载解压mysql文件之后,中间出现了一些问题,终于解决,希望能帮助到需要的朋友. mysql官网下载地址:https://dev.mysql.com/downloads/mysql/点击打开链接 以 ...
- 老哈佛H3的空调控制器的维修记录
这部哈弗车是2005年出厂的,应该差不多是第一批了吧,小毛病很多了.夏天到了,空调也不能启动,灯不亮,按键没反应令我很是着急.于是开始研究. 这款空调的控制器在cd机中控下面,需要拆中控取出.是一个黑 ...
- laravel路由与控制器(资源路由restful)
目前我们大致了解了laravel下,在开始一个Http程序需要先定义路由.之前的例子中,我们的业务逻辑都是在路由里实现,这对于简单的网站或web应用没什么问题,当我们需要扩大规模,程序变得复杂,分层的 ...
- 转载: Java虚拟机:运行时内存数据区域、对象内存分配与访问
转载: https://blog.csdn.net/a745233700/article/details/80291694 (虽然大部分内容都其实是深入理解jvm虚拟机这本书里的,不过整理的很牛逼 ...
- sql serverDB运维实用sql大全
运维sql server的sql总结,包含阻塞语句.等待语句.某个时间段的sql性能查询等等常用sql语句 ##断开库的连接,记得修改库名 USE masterGOALTER DATABASE [DB ...
- 每日一练_PAT_B1001
鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”.鲁宾逊先生和多多都很开心,因为花生正是他们的最爱.在 ...
- CCF_ 201512-4_送货
一道拖了很久了题,用bool开的vis不会爆内存,dfs回溯的话会超时,于是有了一个很巧妙的dfs. #include<iostream> #include<cstring> ...
- 小白学 Python 数据分析(3):Pandas (二)数据结构 Series
在家为国家做贡献太无聊,不如跟我一起学点 Python 顺便问一下,你们都喜欢什么什么样的文章封面图,老用这一张感觉有点丑 人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析( ...