关于创建泛型类

cygzappo 2007-10-01 01:24:30
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).有什么用处吗?

假如实现一个没有提供整形参数的对象。那这几个构造函数的,执行顺序是什么呢?
假如实现一个提供了整形参数作为集合容量的对象,那么这几个构造函数的,执行顺序是什么呢?

多谢了
...全文
67 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyi8808 2007-10-01
  • 打赏
  • 举报
回复
public ShortCollection(int size)
{
maxSize = size;
innerCollection = new Collection <T >();
}

生成一个最大尺寸为 size 的空集合。
wuyi8808 2007-10-01
  • 打赏
  • 举报
回复

public ShortCollection(List <T > list) : this(10, list)
{
}

表示如果仅以 List <T> 参数调用构造函数,则用 size=10, 及 list 调用
public ShortCollection(int size, List <T > list)
这个构造函数。
wuyi8808 2007-10-01
  • 打赏
  • 举报
回复
public ShortCollection() : this(10)
{
}

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

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧