C# List 接口 ICollection<T> 、ICollection 源码分析

  书接上文

   一、 ICollection 是什么?

     定义所有非泛型集合的大小、枚举数和同步方法

    接口成员函数

 public interface ICollection : IEnumerable
    {
        // 摘要: 
        //     获取 System.Collections.ICollection 中包含的元素数。
        //
        // 返回结果: 
        //     System.Collections.ICollection 中包含的元素个数。
        int Count { get; }
        //
        // 摘要: 
        //     获取一个值,该值指示是否同步对 System.Collections.ICollection 的访问(线程安全)。
        //
        // 返回结果: 
        //     如果对 System.Collections.ICollection 的访问是同步的(线程安全),则为 true;否则为 false。
        bool IsSynchronized { get; }
        //
        // 摘要: 
        //     获取可用于同步对 System.Collections.ICollection 的访问的对象。
        //
        // 返回结果: 
        //     可用于同步对 System.Collections.ICollection 的访问的对象。
        object SyncRoot { get; }

        // 摘要: 
        //     从特定的 System.Array 索引处开始,将 System.Collections.ICollection 的元素复制到一个 System.Array
        //     中。
        //
        // 参数: 
        //   array:
        //     作为从 System.Collections.ICollection 复制的元素的目标的一维 System.Array。 System.Array
        //     必须具有从零开始的索引。
        //
        //   index:
        //     array 中从零开始的索引,从此索引处开始进行复制。
        //
        // 异常: 
        //   System.ArgumentNullException:
        //     array 为 null。
        //
        //   System.ArgumentOutOfRangeException:
        //     index 小于零。
        //
        //   System.ArgumentException:
        //     array 是多维的。 - 或 - 源 System.Collections.ICollection 中的元素数大于从 index 到目标 array
        //     结尾处之间的可用空间。 - 或 - 源 System.Collections.ICollection 的类型无法自动转换为目标 array 的类型。
        void CopyTo(Array array, int index);
    }
}

 

1. Count实现

public int Count
{
	[__DynamicallyInvokable]
	get
	{
		return this._size;
	}
}

 

_size是List 私有变量,表示集合的元素的数量,在这里提一下.net 的代码做大量防呆下面可以看一下


[__DynamicallyInvokable]

public void Add(T item)

{

	if (this._size == this._items.Length)

	{

		this.EnsureCapacity(this._size + 1);

	}

	T[] items = this._items;

	int size = this._size;

	this._size = size + 1;

	items[size] = item;

	this._version++;

}
private void EnsureCapacity(int min)
{
	if (this._items.Length < min)
	{
		int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
		if (num > 2146435071)
		{
			num = 2146435071;
		}
		if (num < min)
		{
			num = min;
		}
		this.Capacity = num;
	}
}

_size可是有上限的哦。

 

private T[] _items;  看到这里应该明白List 摆弄还是数组 只是继承一系列的接口,实现对数组操作的函数,List的私有变量

 

 

2.syncRoot的实现  获取可用于同步对 ICollection 的访问的对象。

// System.Collections.Generic.List<T>
// Token: 0x170008DE RID: 2270
// (get) Token: 0x060039C0 RID: 14784 RVA: 0x000DAFDD File Offset: 0x000D91DD
[__DynamicallyInvokable]
object ICollection.SyncRoot
{
	[__DynamicallyInvokable]
	get
	{
		if (this._syncRoot == null)
		{
			Interlocked.CompareExchange<object>(ref this._syncRoot, new object(), null);
		}
		return this._syncRoot;
	}
}

3.  IsSynchronized 的实现

获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。

 

二、ICollection<T>的接口

 ICollection<T>就与 ICollection有很大的区别

与IEnumerable,IEnumerable<T> 有很大的区别,IEnumerable<T> 是从IEnumerable继承的,ICollection<T> 、ICollection没有继承关系。

    public interface ICollection<T> : IEnumerable<T>, IEnumerable
    {
        // 摘要: 
        //     获取 System.Collections.Generic.ICollection<T> 中包含的元素数。
        //
        // 返回结果: 
        //     System.Collections.Generic.ICollection<T> 中包含的元素个数。
        int Count { get; }
        //
        // 摘要: 
        //     获取一个值,该值指示 System.Collections.Generic.ICollection<T> 是否为只读。
        //
        // 返回结果: 
        //     如果 System.Collections.Generic.ICollection<T> 为只读,则为 true;否则为 false。
        bool IsReadOnly { get; }

        // 摘要: 
        //     将某项添加到 System.Collections.Generic.ICollection<T> 中。
        //
        // 参数: 
        //   item:
        //     要添加到 System.Collections.Generic.ICollection<T> 的对象。
        //
        // 异常: 
        //   System.NotSupportedException:
        //     System.Collections.Generic.ICollection<T> 为只读。
        void Add(T item);
        //
        // 摘要: 
        //     从 System.Collections.Generic.ICollection<T> 中移除所有项。
        //
        // 异常: 
        //   System.NotSupportedException:
        //     System.Collections.Generic.ICollection<T> 为只读。
        void Clear();
        //
        // 摘要: 
        //     确定 System.Collections.Generic.ICollection<T> 是否包含特定值。
        //
        // 参数: 
        //   item:
        //     要在 System.Collections.Generic.ICollection<T> 中定位的对象。
        //
        // 返回结果: 
        //     如果在 System.Collections.Generic.ICollection<T> 中找到 item,则为 true;否则为 false。
        bool Contains(T item);
        //
        // 摘要: 
        //     从特定的 System.Array 索引开始,将 System.Collections.Generic.ICollection<T> 的元素复制到一个
        //     System.Array 中。
        //
        // 参数: 
        //   array:
        //     作为从 System.Collections.Generic.ICollection<T> 复制的元素的目标的一维 System.Array。 System.Array
        //     必须具有从零开始的索引。
        //
        //   arrayIndex:
        //     array 中从零开始的索引,从此索引处开始进行复制。
        //
        // 异常: 
        //   System.ArgumentNullException:
        //     array 为 null。
        //
        //   System.ArgumentOutOfRangeException:
        //     arrayIndex 小于 0。
        //
        //   System.ArgumentException:
        //     源 System.Collections.Generic.ICollection<T> 中的元素数目大于从 arrayIndex 到目标 array
        //     末尾之间的可用空间。
        void CopyTo(T[] array, int arrayIndex);
        //
        // 摘要: 
        //     从 System.Collections.Generic.ICollection<T> 中移除特定对象的第一个匹配项。
        //
        // 参数: 
        //   item:
        //     要从 System.Collections.Generic.ICollection<T> 中移除的对象。
        //
        // 返回结果: 
        //     如果已从 System.Collections.Generic.ICollection<T> 中成功移除 item,则为 true;否则为 false。
        //     如果在原始 System.Collections.Generic.ICollection<T> 中没有找到 item,该方法也会返回 false。
        //
        // 异常: 
        //   System.NotSupportedException:
        //     System.Collections.Generic.ICollection<T> 为只读。
        bool Remove(T item);
    }

ICollection<T>定义操作泛型集合的方法。

接口函数( Count、 IsReadOnly不多介绍)

1.Add(T item)实现

[__DynamicallyInvokable]
public void Add(T item)
{
	if (this._size == this._items.Length)
	{
		this.EnsureCapacity(this._size + 1);
	}
	T[] items = this._items;
	int size = this._size;
	this._size = size + 1;
	items[size] = item;
	this._version++;
}

2.Clear()实现

[__DynamicallyInvokable]
public void Clear()
{
	if (this._size > 0)
	{
		Array.Clear(this._items, 0, this._size);
		this._size = 0;
	}
	this._version++;
}

3.Contrains(T item)的实现

[__DynamicallyInvokable]
public bool Contains(T item)
{
	if (item == null)
	{
		for (int i = 0; i < this._size; i++)
		{
			if (this._items[i] == null)
			{
				return true;
			}
		}
		return false;
	}
	EqualityComparer<T> @default = EqualityComparer<T>.Default;
	for (int j = 0; j < this._size; j++)
	{
		if (@default.Equals(this._items[j], item))
		{
			return true;
		}
	}
	return false;
}

4.CopyTo(T item)的实现 

// Token: 0x060039D4 RID: 14804 RVA: 0x000DB381 File Offset: 0x000D9581
[__DynamicallyInvokable]
public void CopyTo(T[] array, int arrayIndex)
{
	Array.Copy(this._items, 0, array, arrayIndex, this._size);
}

这里看得出来List 的数组添加删除是Array这个类,随手弄一下,主要目的还学一下.net 的架构,提高自己代码水平

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值