C# List接口源码分析 List 与ArrayList区别

1.List     T[]

   ArrayList   object[]

   ArrayList  存在装箱与拆箱 性能有损

2.Capactiy 的集合大小

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;
	}
}
[__DynamicallyInvokable]
public int Capacity
{
	[__DynamicallyInvokable]
	get
	{
		return this._items.Length;
	}
	[__DynamicallyInvokable]
	set
	{
		if (value < this._size)
		{
			ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
		}
		if (value != this._items.Length)
		{
			if (value > 0)
			{
				T[] array = new T[value];
				if (this._size > 0)
				{
					Array.Copy(this._items, 0, array, 0, this._size);
				}
				this._items = array;
				return;
			}
			this._items = List._emptyArray;
		}
	}
}

 

 

 

你可能感兴趣的:(编程)