gpt4 book ai didi

c# - List的实现是什么?

转载 作者:太空狗 更新时间:2023-10-29 23:58:05 27 4
gpt4 key购买 nike

我读了这段代码:

List<long> userIdList = new List<long>();

但是我跳转到了 List 的定义(使用 VS2012)(在 System.Collections.Generic 中),我发现:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
// Summary:
// Initializes a new instance of the System.Collections.Generic.List<T> class
// that is empty and has the default initial capacity.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public List();
//
// Summary:
// Initializes a new instance of the System.Collections.Generic.List<T> class
// that contains elements copied from the specified collection and has sufficient
// capacity to accommodate the number of elements copied.
//
// Parameters:
// collection:
// The collection whose elements are copied to the new list.
//
// Exceptions:
// System.ArgumentNullException:
// collection is null.
public List(IEnumerable<T> collection);
//
// Summary:
// Initializes a new instance of the System.Collections.Generic.List<T> class
// that is empty and has the specified initial capacity.
//
// Parameters:
// capacity:
// The number of elements that the new list can initially store.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// capacity is less than 0.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public List(int capacity);

// Summary:
// Gets or sets the total number of elements the internal data structure can
// hold without resizing.
//
// Returns:
// The number of elements that the System.Collections.Generic.List<T> can contain
// before resizing is required.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// System.Collections.Generic.List<T>.Capacity is set to a value that is less
// than System.Collections.Generic.List<T>.Count.
//
// System.OutOfMemoryException:
// There is not enough memory available on the system.
public int Capacity { get; set; }
//
// Summary:
// Gets the number of elements actually contained in the System.Collections.Generic.List<T>.
//
// Returns:
// The number of elements actually contained in the System.Collections.Generic.List<T>.
public int Count { get; }

// Summary:
// Gets or sets the element at the specified index.
//
// Parameters:
// index:
// The zero-based index of the element to get or set.
//
// Returns:
// The element at the specified index.
//
// Exceptions:
// System.ArgumentOutOfRangeException:
// index is less than 0.-or-index is equal to or greater than System.Collections.Generic.List<T>.Count.
public T this[int index] { get; set; }

// Summary:
// Adds an object to the end of the System.Collections.Generic.List<T>.
//
// Parameters:
// item:
// The object to be added to the end of the System.Collections.Generic.List<T>.
// The value can be null for reference types.
public void Add(T item);

...

它不是接口(interface)或抽象,但它没有函数体(对于该类中的任何方法)。我知道ArrayListLinkedList,但是对于List,我不知道它的实现。

我的问题:

  1. List的实现在哪里?
  2. 如果 List 等于 ArrayList 或其他东西,为什么 .net 允许两个类,它们的功能相同但名称不同?如果 List 不等于 .NET 中的任何其他类,那么为什么要给它起这样一个模棱两可的名称?

MSDN states :

The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface by using an array whose size is dynamically increased as required.

所以,我认为这是一个糟糕的名字......

最佳答案

执行List<T>无法从 Visual Studio 中显示,因为它不存在源代码。它仅显示类的概要(这就是为什么 Visual Studio 在按 F12 时将 [元数据] 放在“代码文件”的顶部)。

实际来源可以在referencesource.microsoft.com上找到.

If List equals ArrayList or something, why .net will allow two class which equals function but different name? If List doesn't equal any other class in .NET, so why give it such an ambiguous name?

不,它们不一样。 ArrayList是一个非泛型列表实现,而 List<T> generic , 因此是强类型的。

关于模棱两可的名称:我认为微软的命名是正确的 List . ArrayList无论如何,这是一个可怕的名字。太强调执行了。你不关心它后面有一个数组:对你来说它只是一个 List .鉴于该名称可用,这是一个不错的名称选择。

关于c# - List的实现是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31286005/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com