public class ShortCollection<T> : IList<T>
{
  protected Collection<T> innerCollection;
  protected int maxSize = 10;
  public ShortCollection() : this(10)
   {
   }
  public ShortCollection(int size)
   {
     maxSize = size;
     innerCollection = new Collection<T>();
   }
  public ShortCollection(List<T> list) : this(10, list)
   {
   }
  public ShortCollection(int size, List<T> list)
   {
     maxSize = size;
     if (list.Count <= maxSize)
      {
       innerCollection = new Collection<T>(list);
      }
     else
      {
       ThrowTooManyItemsException();
      }
   }
  protected void ThrowTooManyItemsException()
   {
     throw new IndexOutOfRangeException(
     "Unable to add any more items, maximum size is " +
     maxSize.ToString()
     + " items.");
   }
为什么需要那么多的构造函数呢?
  public ShortCollection() : this(10)
   {
   }
  public ShortCollection(int size)
   {
     maxSize = size;
     innerCollection = new Collection<T>();
   }
这两个起到什么作用了?public ShortCollection(List<T> list) : this(10, list)
   {
   }
这个构造函数,没有函数体,只是知道签名是(10,list).有什么用处吗?假如实现一个没有提供整形参数的对象。那这几个构造函数的,执行顺序是什么呢?
假如实现一个提供了整形参数作为集合容量的对象,那么这几个构造函数的,执行顺序是什么呢?多谢了

解决方案 »

  1.   

      public ShortCollection() : this(10) 
       { 
       } 表示如果是无参调用构造函数,则以 size=10 调用 public ShortCollection(int size) 这个构造函数。
      

  2.   


       public ShortCollection(List <T > list) : this(10, list) 
       { 
       } 表示如果仅以 List <T> 参数调用构造函数,则用 size=10, 及 list 调用
     public ShortCollection(int size, List <T > list) 
    这个构造函数。
      

  3.   

       public ShortCollection(int size) 
       { 
         maxSize = size; 
         innerCollection = new Collection <T >(); 
       } 生成一个最大尺寸为 size 的空集合。