日期:2014-05-18  浏览次数:20816 次

List<T>源代码不懂的地方
我是C#的初学者,现在正在学习泛型,是拿.ner framework的类库中的List<T>类的源代码做例子,下面是源代码
有几行看不懂,望高手指教,

  [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))] 
  [DebuggerDisplay("Count = {Count}")]
  [Serializable] 
 
  public class List<T> : IList<T>, System.Collections.IList
  { 
  private const int _defaultCapacity = 4;//默认容量
  private T[] _items;
  [ContractPublicPropertyName("Count")] 
  private int _size;
  private int _version; 
  [NonSerialized] 
  private Object _syncRoot;
  static readonly T[] _emptyArray = new T[0];

  // 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.

#if !FEATURE_CORECLR 
  [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] 
#endif
  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. 
#if !FEATURE_CORECLR
  [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] 
#endif
  public List(int capacity) {
  if (capacity < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, 

ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  Contract.EndContractBlock(); 
  _items = new T[capacity];
  } 
====================================
以下几行代码看不懂,望高手指点这几行是分别什么意思
 [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))] 
  [DebuggerDisplay("Count = {Count}")]
  [Serializable] 

#if !FEATURE_CORECLR 
  [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]

------解决方案--------------------
都是特性 
[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")]//上面两个是给Visual studio看的 帮助你调试
[Serializable] //可序列化
 [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]//没怎么看懂,好像是说如果对性能要求很高,可以通过NGen这个本地代码生成器把方法内联
------解决方案--------------------
坦率地说这些你看不懂也不要紧。