.Net自从2.0以后开始支持泛型。

泛型的作用:可以创建独立于被包含类型的类和方法。泛型类使用泛型类型,并可以根据需要使用特定的类型替换泛型类型。这就保证了类型安全性:如果某个类型不支持泛型类,编译器就会出现错误。

不多说,先记录一下源码:

  1. // ==++==
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // ==--==
  6. /*============================================================
  7. **
  8. ** Class: List
  9. **
  10. ** <OWNER>Microsoft</OWNER>
  11. **
  12. ** Purpose: Implements a generic, dynamically sized list as an
  13. ** array.
  14. **
  15. **
  16. ===========================================================*/
  17. namespace System.Collections.Generic {
  18.  
  19. using System;
  20. using System.Runtime;
  21. using System.Runtime.Versioning;
  22. using System.Diagnostics;
  23. using System.Diagnostics.Contracts;
  24. using System.Collections.ObjectModel;
  25. using System.Security.Permissions;
  26.  
  27. // Implements a variable-size List that uses an array of objects to store the
  28. // elements. A List has a capacity, which is the allocated length
  29. // of the internal array. As elements are added to a List, the capacity
  30. // of the List is automatically increased as required by reallocating the
  31. // internal array.
  32. //
  33. [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
  34. [DebuggerDisplay("Count = {Count}")]
  35. [Serializable]
  36. public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
  37. {
  38. private const int _defaultCapacity = ;
  39.  
  40. private T[] _items;
  41. [ContractPublicPropertyName("Count")]
  42. private int _size;
  43. private int _version;
  44. [NonSerialized]
  45. private Object _syncRoot;
  46.  
  47. static readonly T[] _emptyArray = new T[];
  48.  
  49. // Constructs a List. The list is initially empty and has a capacity
  50. // of zero. Upon adding the first element to the list the capacity is
  51. // increased to 16, and then increased in multiples of two as required.
  52. public List() {
  53. _items = _emptyArray;
  54. }
  55.  
  56. // Constructs a List with a given initial capacity. The list is
  57. // initially empty, but will have room for the given number of elements
  58. // before any reallocations are required.
  59. //
  60. public List(int capacity) {
  61. if (capacity < ) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  62. Contract.EndContractBlock();
  63.  
  64. if (capacity == )
  65. _items = _emptyArray;
  66. else
  67. _items = new T[capacity];
  68. }
  69.  
  70. // Constructs a List, copying the contents of the given collection. The
  71. // size and capacity of the new list will both be equal to the size of the
  72. // given collection.
  73. //
  74. public List(IEnumerable<T> collection) {
  75. if (collection==null)
  76. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
  77. Contract.EndContractBlock();
  78.  
  79. ICollection<T> c = collection as ICollection<T>;
  80. if( c != null) {
  81. int count = c.Count;
  82. if (count == )
  83. {
  84. _items = _emptyArray;
  85. }
  86. else {
  87. _items = new T[count];
  88. c.CopyTo(_items, );
  89. _size = count;
  90. }
  91. }
  92. else {
  93. _size = ;
  94. _items = _emptyArray;
  95. // This enumerable could be empty. Let Add allocate a new array, if needed.
  96. // Note it will also go to _defaultCapacity first, not 1, then 2, etc.
  97.  
  98. using(IEnumerator<T> en = collection.GetEnumerator()) {
  99. while(en.MoveNext()) {
  100. Add(en.Current);
  101. }
  102. }
  103. }
  104. }
  105.  
  106. // Gets and sets the capacity of this list. The capacity is the size of
  107. // the internal array used to hold items. When set, the internal
  108. // array of the list is reallocated to the given capacity.
  109. //
  110. public int Capacity {
  111. get {
  112. Contract.Ensures(Contract.Result<int>() >= );
  113. return _items.Length;
  114. }
  115. set {
  116. if (value < _size) {
  117. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
  118. }
  119. Contract.EndContractBlock();
  120.  
  121. if (value != _items.Length) {
  122. if (value > ) {
  123. T[] newItems = new T[value];
  124. if (_size > ) {
  125. Array.Copy(_items, , newItems, , _size);
  126. }
  127. _items = newItems;
  128. }
  129. else {
  130. _items = _emptyArray;
  131. }
  132. }
  133. }
  134. }
  135.  
  136. // Read-only property describing how many elements are in the List.
  137. public int Count {
  138. get {
  139. Contract.Ensures(Contract.Result<int>() >= );
  140. return _size;
  141. }
  142. }
  143.  
  144. bool System.Collections.IList.IsFixedSize {
  145. get { return false; }
  146. }
  147.  
  148. // Is this List read-only?
  149. bool ICollection<T>.IsReadOnly {
  150. get { return false; }
  151. }
  152.  
  153. bool System.Collections.IList.IsReadOnly {
  154. get { return false; }
  155. }
  156.  
  157. // Is this List synchronized (thread-safe)?
  158. bool System.Collections.ICollection.IsSynchronized {
  159. get { return false; }
  160. }
  161.  
  162. // Synchronization root for this object.
  163. Object System.Collections.ICollection.SyncRoot {
  164. get {
  165. if( _syncRoot == null) {
  166. System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
  167. }
  168. return _syncRoot;
  169. }
  170. }
  171. // Sets or Gets the element at the given index.
  172. //
  173. public T this[int index] {
  174. get {
  175. // Following trick can reduce the range check by one
  176. if ((uint) index >= (uint)_size) {
  177. ThrowHelper.ThrowArgumentOutOfRangeException();
  178. }
  179. Contract.EndContractBlock();
  180. return _items[index];
  181. }
  182.  
  183. set {
  184. if ((uint) index >= (uint)_size) {
  185. ThrowHelper.ThrowArgumentOutOfRangeException();
  186. }
  187. Contract.EndContractBlock();
  188. _items[index] = value;
  189. _version++;
  190. }
  191. }
  192.  
  193. private static bool IsCompatibleObject(object value) {
  194. // Non-null values are fine. Only accept nulls if T is a class or Nullable<U>.
  195. // Note that default(T) is not equal to null for value types except when T is Nullable<U>.
  196. return ((value is T) || (value == null && default(T) == null));
  197. }
  198.  
  199. Object System.Collections.IList.this[int index] {
  200. get {
  201. return this[index];
  202. }
  203. set {
  204. ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
  205.  
  206. try {
  207. this[index] = (T)value;
  208. }
  209. catch (InvalidCastException) {
  210. ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
  211. }
  212. }
  213. }
  214.  
  215. // Adds the given object to the end of this list. The size of the list is
  216. // increased by one. If required, the capacity of the list is doubled
  217. // before adding the new element.
  218. //
  219. public void Add(T item) {
  220. if (_size == _items.Length) EnsureCapacity(_size + );
  221. _items[_size++] = item;
  222. _version++;
  223. }
  224.  
  225. int System.Collections.IList.Add(Object item)
  226. {
  227. ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
  228.  
  229. try {
  230. Add((T) item);
  231. }
  232. catch (InvalidCastException) {
  233. ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
  234. }
  235.  
  236. return Count - ;
  237. }
  238.  
  239. // Adds the elements of the given collection to the end of this list. If
  240. // required, the capacity of the list is increased to twice the previous
  241. // capacity or the new size, whichever is larger.
  242. //
  243. public void AddRange(IEnumerable<T> collection) {
  244. Contract.Ensures(Count >= Contract.OldValue(Count));
  245.  
  246. InsertRange(_size, collection);
  247. }
  248.  
  249. public ReadOnlyCollection<T> AsReadOnly() {
  250. Contract.Ensures(Contract.Result<ReadOnlyCollection<T>>() != null);
  251. return new ReadOnlyCollection<T>(this);
  252. }
  253.  
  254. // Searches a section of the list for a given element using a binary search
  255. // algorithm. Elements of the list are compared to the search value using
  256. // the given IComparer interface. If comparer is null, elements of
  257. // the list are compared to the search value using the IComparable
  258. // interface, which in that case must be implemented by all elements of the
  259. // list and the given search value. This method assumes that the given
  260. // section of the list is already sorted; if this is not the case, the
  261. // result will be incorrect.
  262. //
  263. // The method returns the index of the given value in the list. If the
  264. // list does not contain the given value, the method returns a negative
  265. // integer. The bitwise complement operator (~) can be applied to a
  266. // negative result to produce the index of the first element (if any) that
  267. // is larger than the given search value. This is also the index at which
  268. // the search value should be inserted into the list in order for the list
  269. // to remain sorted.
  270. //
  271. // The method uses the Array.BinarySearch method to perform the
  272. // search.
  273. //
  274. public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
  275. if (index < )
  276. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  277. if (count < )
  278. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  279. if (_size - index < count)
  280. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  281. Contract.Ensures(Contract.Result<int>() <= index + count);
  282. Contract.EndContractBlock();
  283.  
  284. return Array.BinarySearch<T>(_items, index, count, item, comparer);
  285. }
  286.  
  287. public int BinarySearch(T item)
  288. {
  289. Contract.Ensures(Contract.Result<int>() <= Count);
  290. return BinarySearch(, Count, item, null);
  291. }
  292.  
  293. public int BinarySearch(T item, IComparer<T> comparer)
  294. {
  295. Contract.Ensures(Contract.Result<int>() <= Count);
  296. return BinarySearch(, Count, item, comparer);
  297. }
  298.  
  299. // Clears the contents of List.
  300. public void Clear() {
  301. if (_size > )
  302. {
  303. Array.Clear(_items, , _size); // Don't need to doc this but we clear the elements so that the gc can reclaim the references.
  304. _size = ;
  305. }
  306. _version++;
  307. }
  308.  
  309. // Contains returns true if the specified element is in the List.
  310. // It does a linear, O(n) search. Equality is determined by calling
  311. // item.Equals().
  312. //
  313. public bool Contains(T item) {
  314. if ((Object) item == null) {
  315. for(int i=; i<_size; i++)
  316. if ((Object) _items[i] == null)
  317. return true;
  318. return false;
  319. }
  320. else {
  321. EqualityComparer<T> c = EqualityComparer<T>.Default;
  322. for(int i=; i<_size; i++) {
  323. if (c.Equals(_items[i], item)) return true;
  324. }
  325. return false;
  326. }
  327. }
  328.  
  329. bool System.Collections.IList.Contains(Object item)
  330. {
  331. if(IsCompatibleObject(item)) {
  332. return Contains((T) item);
  333. }
  334. return false;
  335. }
  336.  
  337. public List<TOutput> ConvertAll<TOutput>(Converter<T,TOutput> converter) {
  338. if( converter == null) {
  339. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
  340. }
  341. // @
  342.  
  343. Contract.EndContractBlock();
  344.  
  345. List<TOutput> list = new List<TOutput>(_size);
  346. for( int i = ; i< _size; i++) {
  347. list._items[i] = converter(_items[i]);
  348. }
  349. list._size = _size;
  350. return list;
  351. }
  352.  
  353. // Copies this List into array, which must be of a
  354. // compatible array type.
  355. //
  356. public void CopyTo(T[] array) {
  357. CopyTo(array, );
  358. }
  359.  
  360. // Copies this List into array, which must be of a
  361. // compatible array type.
  362. //
  363. void System.Collections.ICollection.CopyTo(Array array, int arrayIndex) {
  364. if ((array != null) && (array.Rank != )) {
  365. ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
  366. }
  367. Contract.EndContractBlock();
  368.  
  369. try {
  370. // Array.Copy will check for NULL.
  371. Array.Copy(_items, , array, arrayIndex, _size);
  372. }
  373. catch(ArrayTypeMismatchException){
  374. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
  375. }
  376. }
  377.  
  378. // Copies a section of this list to the given array at the given index.
  379. //
  380. // The method uses the Array.Copy method to copy the elements.
  381. //
  382. public void CopyTo(int index, T[] array, int arrayIndex, int count) {
  383. if (_size - index < count) {
  384. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  385. }
  386. Contract.EndContractBlock();
  387.  
  388. // Delegate rest of error checking to Array.Copy.
  389. Array.Copy(_items, index, array, arrayIndex, count);
  390. }
  391.  
  392. public void CopyTo(T[] array, int arrayIndex) {
  393. // Delegate rest of error checking to Array.Copy.
  394. Array.Copy(_items, , array, arrayIndex, _size);
  395. }
  396.  
  397. // Ensures that the capacity of this list is at least the given minimum
  398. // value. If the currect capacity of the list is less than min, the
  399. // capacity is increased to twice the current capacity or to min,
  400. // whichever is larger.
  401. private void EnsureCapacity(int min) {
  402. if (_items.Length < min) {
  403. int newCapacity = _items.Length == ? _defaultCapacity : _items.Length * ;
  404. // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
  405. // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
  406. if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
  407. if (newCapacity < min) newCapacity = min;
  408. Capacity = newCapacity;
  409. }
  410. }
  411.  
  412. public bool Exists(Predicate<T> match) {
  413. return FindIndex(match) != -;
  414. }
  415.  
  416. public T Find(Predicate<T> match) {
  417. if( match == null) {
  418. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  419. }
  420. Contract.EndContractBlock();
  421.  
  422. for(int i = ; i < _size; i++) {
  423. if(match(_items[i])) {
  424. return _items[i];
  425. }
  426. }
  427. return default(T);
  428. }
  429.  
  430. public List<T> FindAll(Predicate<T> match) {
  431. if( match == null) {
  432. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  433. }
  434. Contract.EndContractBlock();
  435.  
  436. List<T> list = new List<T>();
  437. for(int i = ; i < _size; i++) {
  438. if(match(_items[i])) {
  439. list.Add(_items[i]);
  440. }
  441. }
  442. return list;
  443. }
  444.  
  445. public int FindIndex(Predicate<T> match) {
  446. Contract.Ensures(Contract.Result<int>() >= -);
  447. Contract.Ensures(Contract.Result<int>() < Count);
  448. return FindIndex(, _size, match);
  449. }
  450.  
  451. public int FindIndex(int startIndex, Predicate<T> match) {
  452. Contract.Ensures(Contract.Result<int>() >= -);
  453. Contract.Ensures(Contract.Result<int>() < startIndex + Count);
  454. return FindIndex(startIndex, _size - startIndex, match);
  455. }
  456.  
  457. public int FindIndex(int startIndex, int count, Predicate<T> match) {
  458. if( (uint)startIndex > (uint)_size ) {
  459. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
  460. }
  461.  
  462. if (count < || startIndex > _size - count) {
  463. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
  464. }
  465.  
  466. if( match == null) {
  467. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  468. }
  469. Contract.Ensures(Contract.Result<int>() >= -);
  470. Contract.Ensures(Contract.Result<int>() < startIndex + count);
  471. Contract.EndContractBlock();
  472.  
  473. int endIndex = startIndex + count;
  474. for( int i = startIndex; i < endIndex; i++) {
  475. if( match(_items[i])) return i;
  476. }
  477. return -;
  478. }
  479.  
  480. public T FindLast(Predicate<T> match) {
  481. if( match == null) {
  482. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  483. }
  484. Contract.EndContractBlock();
  485.  
  486. for(int i = _size - ; i >= ; i--) {
  487. if(match(_items[i])) {
  488. return _items[i];
  489. }
  490. }
  491. return default(T);
  492. }
  493.  
  494. public int FindLastIndex(Predicate<T> match) {
  495. Contract.Ensures(Contract.Result<int>() >= -);
  496. Contract.Ensures(Contract.Result<int>() < Count);
  497. return FindLastIndex(_size - , _size, match);
  498. }
  499.  
  500. public int FindLastIndex(int startIndex, Predicate<T> match) {
  501. Contract.Ensures(Contract.Result<int>() >= -);
  502. Contract.Ensures(Contract.Result<int>() <= startIndex);
  503. return FindLastIndex(startIndex, startIndex + , match);
  504. }
  505.  
  506. public int FindLastIndex(int startIndex, int count, Predicate<T> match) {
  507. if( match == null) {
  508. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  509. }
  510. Contract.Ensures(Contract.Result<int>() >= -);
  511. Contract.Ensures(Contract.Result<int>() <= startIndex);
  512. Contract.EndContractBlock();
  513.  
  514. if(_size == ) {
  515. // Special case for 0 length List
  516. if( startIndex != -) {
  517. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
  518. }
  519. }
  520. else {
  521. // Make sure we're not out of range
  522. if ( (uint)startIndex >= (uint)_size) {
  523. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
  524. }
  525. }
  526.  
  527. // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
  528. if (count < || startIndex - count + < ) {
  529. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
  530. }
  531.  
  532. int endIndex = startIndex - count;
  533. for( int i = startIndex; i > endIndex; i--) {
  534. if( match(_items[i])) {
  535. return i;
  536. }
  537. }
  538. return -;
  539. }
  540.  
  541. public void ForEach(Action<T> action) {
  542. if( action == null) {
  543. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  544. }
  545. Contract.EndContractBlock();
  546.  
  547. int version = _version;
  548.  
  549. for(int i = ; i < _size; i++) {
  550. if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5) {
  551. break;
  552. }
  553. action(_items[i]);
  554. }
  555.  
  556. if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
  557. ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
  558. }
  559.  
  560. // Returns an enumerator for this list with the given
  561. // permission for removal of elements. If modifications made to the list
  562. // while an enumeration is in progress, the MoveNext and
  563. // GetObject methods of the enumerator will throw an exception.
  564. //
  565. public Enumerator GetEnumerator() {
  566. return new Enumerator(this);
  567. }
  568.  
  569. /// <internalonly/>
  570. IEnumerator<T> IEnumerable<T>.GetEnumerator() {
  571. return new Enumerator(this);
  572. }
  573.  
  574. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
  575. return new Enumerator(this);
  576. }
  577.  
  578. public List<T> GetRange(int index, int count) {
  579. if (index < ) {
  580. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  581. }
  582.  
  583. if (count < ) {
  584. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  585. }
  586.  
  587. if (_size - index < count) {
  588. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  589. }
  590. Contract.Ensures(Contract.Result<List<T>>() != null);
  591. Contract.EndContractBlock();
  592.  
  593. List<T> list = new List<T>(count);
  594. Array.Copy(_items, index, list._items, , count);
  595. list._size = count;
  596. return list;
  597. }
  598.  
  599. // Returns the index of the first occurrence of a given value in a range of
  600. // this list. The list is searched forwards from beginning to end.
  601. // The elements of the list are compared to the given value using the
  602. // Object.Equals method.
  603. //
  604. // This method uses the Array.IndexOf method to perform the
  605. // search.
  606. //
  607. public int IndexOf(T item) {
  608. Contract.Ensures(Contract.Result<int>() >= -);
  609. Contract.Ensures(Contract.Result<int>() < Count);
  610. return Array.IndexOf(_items, item, , _size);
  611. }
  612.  
  613. int System.Collections.IList.IndexOf(Object item)
  614. {
  615. if(IsCompatibleObject(item)) {
  616. return IndexOf((T)item);
  617. }
  618. return -;
  619. }
  620.  
  621. // Returns the index of the first occurrence of a given value in a range of
  622. // this list. The list is searched forwards, starting at index
  623. // index and ending at count number of elements. The
  624. // elements of the list are compared to the given value using the
  625. // Object.Equals method.
  626. //
  627. // This method uses the Array.IndexOf method to perform the
  628. // search.
  629. //
  630. public int IndexOf(T item, int index) {
  631. if (index > _size)
  632. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
  633. Contract.Ensures(Contract.Result<int>() >= -);
  634. Contract.Ensures(Contract.Result<int>() < Count);
  635. Contract.EndContractBlock();
  636. return Array.IndexOf(_items, item, index, _size - index);
  637. }
  638.  
  639. // Returns the index of the first occurrence of a given value in a range of
  640. // this list. The list is searched forwards, starting at index
  641. // index and upto count number of elements. The
  642. // elements of the list are compared to the given value using the
  643. // Object.Equals method.
  644. //
  645. // This method uses the Array.IndexOf method to perform the
  646. // search.
  647. //
  648. public int IndexOf(T item, int index, int count) {
  649. if (index > _size)
  650. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
  651.  
  652. if (count < || index > _size - count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
  653. Contract.Ensures(Contract.Result<int>() >= -);
  654. Contract.Ensures(Contract.Result<int>() < Count);
  655. Contract.EndContractBlock();
  656.  
  657. return Array.IndexOf(_items, item, index, count);
  658. }
  659.  
  660. // Inserts an element into this list at a given index. The size of the list
  661. // is increased by one. If required, the capacity of the list is doubled
  662. // before inserting the new element.
  663. //
  664. public void Insert(int index, T item) {
  665. // Note that insertions at the end are legal.
  666. if ((uint) index > (uint)_size) {
  667. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
  668. }
  669. Contract.EndContractBlock();
  670. if (_size == _items.Length) EnsureCapacity(_size + );
  671. if (index < _size) {
  672. Array.Copy(_items, index, _items, index + , _size - index);
  673. }
  674. _items[index] = item;
  675. _size++;
  676. _version++;
  677. }
  678.  
  679. void System.Collections.IList.Insert(int index, Object item)
  680. {
  681. ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
  682.  
  683. try {
  684. Insert(index, (T) item);
  685. }
  686. catch (InvalidCastException) {
  687. ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
  688. }
  689. }
  690.  
  691. // Inserts the elements of the given collection at a given index. If
  692. // required, the capacity of the list is increased to twice the previous
  693. // capacity or the new size, whichever is larger. Ranges may be added
  694. // to the end of the list by setting index to the List's size.
  695. //
  696. public void InsertRange(int index, IEnumerable<T> collection) {
  697. if (collection==null) {
  698. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
  699. }
  700.  
  701. if ((uint)index > (uint)_size) {
  702. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
  703. }
  704. Contract.EndContractBlock();
  705.  
  706. ICollection<T> c = collection as ICollection<T>;
  707. if( c != null ) { // if collection is ICollection<T>
  708. int count = c.Count;
  709. if (count > ) {
  710. EnsureCapacity(_size + count);
  711. if (index < _size) {
  712. Array.Copy(_items, index, _items, index + count, _size - index);
  713. }
  714.  
  715. // If we're inserting a List into itself, we want to be able to deal with that.
  716. if (this == c) {
  717. // Copy first part of _items to insert location
  718. Array.Copy(_items, , _items, index, index);
  719. // Copy last part of _items back to inserted location
  720. Array.Copy(_items, index+count, _items, index*, _size-index);
  721. }
  722. else {
  723. T[] itemsToInsert = new T[count];
  724. c.CopyTo(itemsToInsert, );
  725. itemsToInsert.CopyTo(_items, index);
  726. }
  727. _size += count;
  728. }
  729. }
  730. else {
  731. using(IEnumerator<T> en = collection.GetEnumerator()) {
  732. while(en.MoveNext()) {
  733. Insert(index++, en.Current);
  734. }
  735. }
  736. }
  737. _version++;
  738. }
  739.  
  740. // Returns the index of the last occurrence of a given value in a range of
  741. // this list. The list is searched backwards, starting at the end
  742. // and ending at the first element in the list. The elements of the list
  743. // are compared to the given value using the Object.Equals method.
  744. //
  745. // This method uses the Array.LastIndexOf method to perform the
  746. // search.
  747. //
  748. public int LastIndexOf(T item)
  749. {
  750. Contract.Ensures(Contract.Result<int>() >= -);
  751. Contract.Ensures(Contract.Result<int>() < Count);
  752. if (_size == ) { // Special case for empty list
  753. return -;
  754. }
  755. else {
  756. return LastIndexOf(item, _size - , _size);
  757. }
  758. }
  759.  
  760. // Returns the index of the last occurrence of a given value in a range of
  761. // this list. The list is searched backwards, starting at index
  762. // index and ending at the first element in the list. The
  763. // elements of the list are compared to the given value using the
  764. // Object.Equals method.
  765. //
  766. // This method uses the Array.LastIndexOf method to perform the
  767. // search.
  768. //
  769. public int LastIndexOf(T item, int index)
  770. {
  771. if (index >= _size)
  772. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
  773. Contract.Ensures(Contract.Result<int>() >= -);
  774. Contract.Ensures(((Count == ) && (Contract.Result<int>() == -)) || ((Count > ) && (Contract.Result<int>() <= index)));
  775. Contract.EndContractBlock();
  776. return LastIndexOf(item, index, index + );
  777. }
  778.  
  779. // Returns the index of the last occurrence of a given value in a range of
  780. // this list. The list is searched backwards, starting at index
  781. // index and upto count elements. The elements of
  782. // the list are compared to the given value using the Object.Equals
  783. // method.
  784. //
  785. // This method uses the Array.LastIndexOf method to perform the
  786. // search.
  787. //
  788. public int LastIndexOf(T item, int index, int count) {
  789. if ((Count != ) && (index < )) {
  790. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  791. }
  792.  
  793. if ((Count !=) && (count < )) {
  794. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  795. }
  796. Contract.Ensures(Contract.Result<int>() >= -);
  797. Contract.Ensures(((Count == ) && (Contract.Result<int>() == -)) || ((Count > ) && (Contract.Result<int>() <= index)));
  798. Contract.EndContractBlock();
  799.  
  800. if (_size == ) { // Special case for empty list
  801. return -;
  802. }
  803.  
  804. if (index >= _size) {
  805. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
  806. }
  807.  
  808. if (count > index + ) {
  809. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
  810. }
  811.  
  812. return Array.LastIndexOf(_items, item, index, count);
  813. }
  814.  
  815. // Removes the element at the given index. The size of the list is
  816. // decreased by one.
  817. //
  818. public bool Remove(T item) {
  819. int index = IndexOf(item);
  820. if (index >= ) {
  821. RemoveAt(index);
  822. return true;
  823. }
  824.  
  825. return false;
  826. }
  827.  
  828. void System.Collections.IList.Remove(Object item)
  829. {
  830. if(IsCompatibleObject(item)) {
  831. Remove((T) item);
  832. }
  833. }
  834.  
  835. // This method removes all items which matches the predicate.
  836. // The complexity is O(n).
  837. public int RemoveAll(Predicate<T> match) {
  838. if( match == null) {
  839. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  840. }
  841. Contract.Ensures(Contract.Result<int>() >= );
  842. Contract.Ensures(Contract.Result<int>() <= Contract.OldValue(Count));
  843. Contract.EndContractBlock();
  844.  
  845. int freeIndex = ; // the first free slot in items array
  846.  
  847. // Find the first item which needs to be removed.
  848. while( freeIndex < _size && !match(_items[freeIndex])) freeIndex++;
  849. if( freeIndex >= _size) return ;
  850.  
  851. int current = freeIndex + ;
  852. while( current < _size) {
  853. // Find the first item which needs to be kept.
  854. while( current < _size && match(_items[current])) current++;
  855.  
  856. if( current < _size) {
  857. // copy item to the free slot.
  858. _items[freeIndex++] = _items[current++];
  859. }
  860. }
  861.  
  862. Array.Clear(_items, freeIndex, _size - freeIndex);
  863. int result = _size - freeIndex;
  864. _size = freeIndex;
  865. _version++;
  866. return result;
  867. }
  868.  
  869. // Removes the element at the given index. The size of the list is
  870. // decreased by one.
  871. //
  872. public void RemoveAt(int index) {
  873. if ((uint)index >= (uint)_size) {
  874. ThrowHelper.ThrowArgumentOutOfRangeException();
  875. }
  876. Contract.EndContractBlock();
  877. _size--;
  878. if (index < _size) {
  879. Array.Copy(_items, index + , _items, index, _size - index);
  880. }
  881. _items[_size] = default(T);
  882. _version++;
  883. }
  884.  
  885. // Removes a range of elements from this list.
  886. //
  887. public void RemoveRange(int index, int count) {
  888. if (index < ) {
  889. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  890. }
  891.  
  892. if (count < ) {
  893. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  894. }
  895.  
  896. if (_size - index < count)
  897. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  898. Contract.EndContractBlock();
  899.  
  900. if (count > ) {
  901. int i = _size;
  902. _size -= count;
  903. if (index < _size) {
  904. Array.Copy(_items, index + count, _items, index, _size - index);
  905. }
  906. Array.Clear(_items, _size, count);
  907. _version++;
  908. }
  909. }
  910.  
  911. // Reverses the elements in this list.
  912. public void Reverse() {
  913. Reverse(, Count);
  914. }
  915.  
  916. // Reverses the elements in a range of this list. Following a call to this
  917. // method, an element in the range given by index and count
  918. // which was previously located at index i will now be located at
  919. // index index + (index + count - i - 1).
  920. //
  921. // This method uses the Array.Reverse method to reverse the
  922. // elements.
  923. //
  924. public void Reverse(int index, int count) {
  925. if (index < ) {
  926. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  927. }
  928.  
  929. if (count < ) {
  930. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  931. }
  932.  
  933. if (_size - index < count)
  934. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  935. Contract.EndContractBlock();
  936. Array.Reverse(_items, index, count);
  937. _version++;
  938. }
  939.  
  940. // Sorts the elements in this list. Uses the default comparer and
  941. // Array.Sort.
  942. public void Sort()
  943. {
  944. Sort(, Count, null);
  945. }
  946.  
  947. // Sorts the elements in this list. Uses Array.Sort with the
  948. // provided comparer.
  949. public void Sort(IComparer<T> comparer)
  950. {
  951. Sort(, Count, comparer);
  952. }
  953.  
  954. // Sorts the elements in a section of this list. The sort compares the
  955. // elements to each other using the given IComparer interface. If
  956. // comparer is null, the elements are compared to each other using
  957. // the IComparable interface, which in that case must be implemented by all
  958. // elements of the list.
  959. //
  960. // This method uses the Array.Sort method to sort the elements.
  961. //
  962. public void Sort(int index, int count, IComparer<T> comparer) {
  963. if (index < ) {
  964. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  965. }
  966.  
  967. if (count < ) {
  968. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  969. }
  970.  
  971. if (_size - index < count)
  972. ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
  973. Contract.EndContractBlock();
  974.  
  975. Array.Sort<T>(_items, index, count, comparer);
  976. _version++;
  977. }
  978.  
  979. public void Sort(Comparison<T> comparison) {
  980. if( comparison == null) {
  981. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  982. }
  983. Contract.EndContractBlock();
  984.  
  985. if( _size > ) {
  986. IComparer<T> comparer = new Array.FunctorComparer<T>(comparison);
  987. Array.Sort(_items, , _size, comparer);
  988. }
  989. }
  990.  
  991. // ToArray returns a new Object array containing the contents of the List.
  992. // This requires copying the List, which is an O(n) operation.
  993. public T[] ToArray() {
  994. Contract.Ensures(Contract.Result<T[]>() != null);
  995. Contract.Ensures(Contract.Result<T[]>().Length == Count);
  996.  
  997. T[] array = new T[_size];
  998. Array.Copy(_items, , array, , _size);
  999. return array;
  1000. }
  1001.  
  1002. // Sets the capacity of this list to the size of the list. This method can
  1003. // be used to minimize a list's memory overhead once it is known that no
  1004. // new elements will be added to the list. To completely clear a list and
  1005. // release all memory referenced by the list, execute the following
  1006. // statements:
  1007. //
  1008. // list.Clear();
  1009. // list.TrimExcess();
  1010. //
  1011. public void TrimExcess() {
  1012. int threshold = (int)(((double)_items.Length) * 0.9);
  1013. if( _size < threshold ) {
  1014. Capacity = _size;
  1015. }
  1016. }
  1017.  
  1018. public bool TrueForAll(Predicate<T> match) {
  1019. if( match == null) {
  1020. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  1021. }
  1022. Contract.EndContractBlock();
  1023.  
  1024. for(int i = ; i < _size; i++) {
  1025. if( !match(_items[i])) {
  1026. return false;
  1027. }
  1028. }
  1029. return true;
  1030. }
  1031.  
  1032. internal static IList<T> Synchronized(List<T> list) {
  1033. return new SynchronizedList(list);
  1034. }
  1035.  
  1036. [Serializable()]
  1037. internal class SynchronizedList : IList<T> {
  1038. private List<T> _list;
  1039. private Object _root;
  1040.  
  1041. internal SynchronizedList(List<T> list) {
  1042. _list = list;
  1043. _root = ((System.Collections.ICollection)list).SyncRoot;
  1044. }
  1045.  
  1046. public int Count {
  1047. get {
  1048. lock (_root) {
  1049. return _list.Count;
  1050. }
  1051. }
  1052. }
  1053.  
  1054. public bool IsReadOnly {
  1055. get {
  1056. return ((ICollection<T>)_list).IsReadOnly;
  1057. }
  1058. }
  1059.  
  1060. public void Add(T item) {
  1061. lock (_root) {
  1062. _list.Add(item);
  1063. }
  1064. }
  1065.  
  1066. public void Clear() {
  1067. lock (_root) {
  1068. _list.Clear();
  1069. }
  1070. }
  1071.  
  1072. public bool Contains(T item) {
  1073. lock (_root) {
  1074. return _list.Contains(item);
  1075. }
  1076. }
  1077.  
  1078. public void CopyTo(T[] array, int arrayIndex) {
  1079. lock (_root) {
  1080. _list.CopyTo(array, arrayIndex);
  1081. }
  1082. }
  1083.  
  1084. public bool Remove(T item) {
  1085. lock (_root) {
  1086. return _list.Remove(item);
  1087. }
  1088. }
  1089.  
  1090. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
  1091. lock (_root) {
  1092. return _list.GetEnumerator();
  1093. }
  1094. }
  1095.  
  1096. IEnumerator<T> IEnumerable<T>.GetEnumerator() {
  1097. lock (_root) {
  1098. return ((IEnumerable<T>)_list).GetEnumerator();
  1099. }
  1100. }
  1101.  
  1102. public T this[int index] {
  1103. get {
  1104. lock(_root) {
  1105. return _list[index];
  1106. }
  1107. }
  1108. set {
  1109. lock(_root) {
  1110. _list[index] = value;
  1111. }
  1112. }
  1113. }
  1114.  
  1115. public int IndexOf(T item) {
  1116. lock (_root) {
  1117. return _list.IndexOf(item);
  1118. }
  1119. }
  1120.  
  1121. public void Insert(int index, T item) {
  1122. lock (_root) {
  1123. _list.Insert(index, item);
  1124. }
  1125. }
  1126.  
  1127. public void RemoveAt(int index) {
  1128. lock (_root) {
  1129. _list.RemoveAt(index);
  1130. }
  1131. }
  1132. }
  1133.  
  1134. [Serializable]
  1135. public struct Enumerator : IEnumerator<T>, System.Collections.IEnumerator
  1136. {
  1137. private List<T> list;
  1138. private int index;
  1139. private int version;
  1140. private T current;
  1141.  
  1142. internal Enumerator(List<T> list) {
  1143. this.list = list;
  1144. index = ;
  1145. version = list._version;
  1146. current = default(T);
  1147. }
  1148.  
  1149. public void Dispose() {
  1150. }
  1151.  
  1152. public bool MoveNext() {
  1153.  
  1154. List<T> localList = list;
  1155.  
  1156. if (version == localList._version && ((uint)index < (uint)localList._size))
  1157. {
  1158. current = localList._items[index];
  1159. index++;
  1160. return true;
  1161. }
  1162. return MoveNextRare();
  1163. }
  1164.  
  1165. private bool MoveNextRare()
  1166. {
  1167. if (version != list._version) {
  1168. ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
  1169. }
  1170.  
  1171. index = list._size + ;
  1172. current = default(T);
  1173. return false;
  1174. }
  1175.  
  1176. public T Current {
  1177. get {
  1178. return current;
  1179. }
  1180. }
  1181.  
  1182. Object System.Collections.IEnumerator.Current {
  1183. get {
  1184. if( index == || index == list._size + ) {
  1185. ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
  1186. }
  1187. return Current;
  1188. }
  1189. }
  1190.  
  1191. void System.Collections.IEnumerator.Reset() {
  1192. if (version != list._version) {
  1193. ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
  1194. }
  1195.  
  1196. index = ;
  1197. current = default(T);
  1198. }
  1199.  
  1200. }
  1201. }
  1202. }

创建一个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泛型的更多相关文章

  1. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

  2. .NET面试题系列[8] - 泛型

    “可变性是以一种类型安全的方式,将一个对象作为另一个对象来使用.“ - Jon Skeet .NET面试题系列目录 .NET面试题系列[1] - .NET框架基础知识(1) .NET面试题系列[2] ...

  3. C#4.0泛型的协变,逆变深入剖析

    C#4.0中有一个新特性:协变与逆变.可能很多人在开发过程中不常用到,但是深入的了解他们,肯定是有好处的. 协变和逆变体现在泛型的接口和委托上面,也就是对泛型参数的声明,可以声明为协变,或者逆变.什么 ...

  4. 编写高质量代码:改善Java程序的151个建议(第7章:泛型和反射___建议106~109)

    建议106:动态代理可以使代理模式更加灵活 Java的反射框架提供了动态代理(Dynamic Proxy)机制,允许在运行期对目标类生成代理,避免重复开发.我们知道一个静态代理是通过主题角色(Prox ...

  5. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  6. C#泛型详解(转)

    初步理解泛型: http://www.cnblogs.com/wilber2013/p/4291435.html 泛型中的类型约束和类型推断 http://www.cnblogs.com/wilber ...

  7. C# 泛型

    C# 泛型 1.定义泛型类 在类定义中包含尖括号语法,即可创建泛型类: class MyGenericClass<T> { //Add code } 其中T可以遵循C#命名规则的任意字符. ...

  8. java8中lambda表达式的应用,以及一些泛型相关

    语法部分就不写了,我们直接抛出一个实际问题,看看java8的这些新特性究竟能给我们带来哪些便利 顺带用到一些泛型编程,一切都是为了简化代码 场景: 一个数据类,用于记录职工信息 public clas ...

  9. java 泛型

    1.Student stu =tool.getObj();右边得到的是Object类型,需要向下转型,强转换. 2. 3. 4.泛型方法不能被静态修饰这样写 5.如果想定义定义静态泛型方法,只能这样写 ...

  10. Java泛型的历史

    为什么Java泛型会有当前的缺陷? 之前的章节里已经说明了Java泛型擦除会导致的问题,C++和C#的泛型都是在运行时存在的,难道Java天然不支持“真正的泛型”吗? 事实上,在Java1.5在200 ...

随机推荐

  1. 分布式缓存Redis的持久化方式RDB和AOF

    一.前言 Redis支持两种方式的持久化,RDB和AOF.RDB会根据指定的规则“定时”将内存中的数据存储到硬盘上,AOF会在每次执行命令后将命令本身记录下来.两种持久化方式可以单独使用其中一种,但更 ...

  2. 矩阵matrix变换的用法(css3属性transform: matrix)

    参数 2D矩阵的表示 matrix(a,b,c,d,e,f),其中6个参数在矩阵的分布: -- -- | a c e | | b d f | | 0 0 1 | -- -- 在CSS3中矩阵的原始值是 ...

  3. Windows 64 位 mysql 5.7以上版本包解压中没有data目录和my-default.ini和my.ini文件以及服务无法启动的解决办法以及修改初始密码的方法

    下载解压mysql文件之后,中间出现了一些问题,终于解决,希望能帮助到需要的朋友. mysql官网下载地址:https://dev.mysql.com/downloads/mysql/点击打开链接 以 ...

  4. 老哈佛H3的空调控制器的维修记录

    这部哈弗车是2005年出厂的,应该差不多是第一批了吧,小毛病很多了.夏天到了,空调也不能启动,灯不亮,按键没反应令我很是着急.于是开始研究. 这款空调的控制器在cd机中控下面,需要拆中控取出.是一个黑 ...

  5. laravel路由与控制器(资源路由restful)

    目前我们大致了解了laravel下,在开始一个Http程序需要先定义路由.之前的例子中,我们的业务逻辑都是在路由里实现,这对于简单的网站或web应用没什么问题,当我们需要扩大规模,程序变得复杂,分层的 ...

  6. 转载: Java虚拟机:运行时内存数据区域、对象内存分配与访问

    转载:  https://blog.csdn.net/a745233700/article/details/80291694  (虽然大部分内容都其实是深入理解jvm虚拟机这本书里的,不过整理的很牛逼 ...

  7. sql serverDB运维实用sql大全

    运维sql server的sql总结,包含阻塞语句.等待语句.某个时间段的sql性能查询等等常用sql语句 ##断开库的连接,记得修改库名 USE masterGOALTER DATABASE [DB ...

  8. 每日一练_PAT_B1001

    鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”.鲁宾逊先生和多多都很开心,因为花生正是他们的最爱.在 ...

  9. CCF_ 201512-4_送货

    一道拖了很久了题,用bool开的vis不会爆内存,dfs回溯的话会超时,于是有了一个很巧妙的dfs. #include<iostream> #include<cstring> ...

  10. 小白学 Python 数据分析(3):Pandas (二)数据结构 Series

    在家为国家做贡献太无聊,不如跟我一起学点 Python 顺便问一下,你们都喜欢什么什么样的文章封面图,老用这一张感觉有点丑 人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析( ...