List的实现是什么? [英] What's the implementation of List?

查看:153
本文介绍了List的实现是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过这段代码:

 列表< long> userIdList =新列表< long>(); 

但我跳到了 List (在 System.Collections.Generic 中),我发现:

  public class List< T> :IList< T>,ICollection< T> ;, IEnumerable< T>,IList,ICollection,IEnumerable 
{
//摘要:
//初始化System.Collections的新实例。 Generic.List< T> class
//它是空的并且具有默认的初始容量。
[TargetedPatchingOptOut(对跨NGEN图片边界内联至关重要的性能)]
public List();
//
//摘要:
//初始化System.Collections.Generic.List的新实例< T>类
//包含从指定集合复制的元素,并且具有足够的
//容量来容纳复制的元素数量。
//
//参数:
//集合:
//元素被复制到新列表的集合。
//
//例外:
// System.ArgumentNullException:
//集合为空。
public List(IEnumerable< T> collection);
//
//摘要:
//初始化System.Collections.Generic.List的新实例< T> class
//是空的并且具有指定的初始容量。
//
//参数:
//容量:
//新列​​表最初可以存储的元素数量。
//
//异常:
// System.ArgumentOutOfRangeException:
//容量小于0.
[TargetedPatchingOptOut(性能对跨越NGen内联至关重要图像边界)]
public List(int capacity);

//摘要:
//获取或设置内部数据结构元素的总数
//保持不调整大小。
//
//返回:
// System.Collections.Generic.List< T>元素的数量可以包含
// //需要调整大小之前。
//
//例外:
// System.ArgumentOutOfRangeException:
// System.Collections.Generic.List< T> .Capacity被设置为一个小于
//比System.Collections.Generic.List< T> .Count。
//
// System.OutOfMemoryException:
//系统上没有足够的可用内存。
public int Capacity {get;组; }
//
//摘要:
//获取实际包含在System.Collections.Generic.List< T>中的元素数量。
//
//返回:
// System.Collections.Generic.List< T>中实际包含的元素数目。
public int Count {get; }

//摘要:
//获取或设置指定索引处的元素。
//
//参数:
//索引:
//要获取或设置的元素的从零开始的索引。
//
//返回:
//指定索引处的元素。
//
//例外:
// System.ArgumentOutOfOrangeRangeException:
//索引小于0或-index等于或大于System.Collections。 Generic.List< T> .Count之间。
public T this [int index] {get;组; }

//摘要:
//将一个对象添加到System.Collections.Generic.List< T>的末尾。
//
//参数:
// item:
//要添加到System.Collections.Generic.List< T>结尾的对象。
//对于引用类型,该值可以为null。
public void Add(T item);

...

它不是Interface或Abstract,没有函数体(对于该类中的任何方法)。我知道 ArrayList LinkedList ,但是对于 List ,I不知道它的实现。



我的问题:


  1. List ?

  2. 如果 List 等于 ArrayList 或者什么,为什么.net将允许两个等同于函数但名称不同的类?如果 List 不等于.NET中的任何其他类,那么为什么要给它一个模糊的名字?

MSDN状态


List类是ArrayList类的通用等效物。它
通过使用一个大小为
的数组根据需要动态增加来实现IList泛型接口。

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

不能从Visual Studio显示,因为它没有源代码。它只是显示了类的轮廓(这就是为什么Visual Studio在按F12时将 [元数据] 放在'代码文件'的顶部)。



实际来源可以在 referencesource.microsoft上找到。
$ b


如果List等于ArrayList或者什么,为什么.net会允许两个等同于函数但名称不同的类?如果List不等于.NET中的任何其他类,那么为什么要给它一个模棱两可的名字?


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



关于不明确的名称:我认为微软的命名是 List 。无论如何, ArrayList 是一个可怕的名字。它强调实施太多。你不关心它背后有一个数组:对你来说它只是一个 List 。鉴于这个名字是可用的,这是一个名字的好选择。


I read this code:

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

But I jumped to the definition(use VS2012) of List (in System.Collections.Generic), I found:

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);

    ...

It's not Interface or Abstract, but it doesn't have function body(for any method in that class). I know ArrayList and LinkedList, but for List, I have no idea about its implementation.

My question:

  1. Where is the implementation of List?
  2. 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?

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.

So, I think it's a bad name...

解决方案

The implementation of List<T> can't be shown from Visual Studio because it doesn't have the source code present. It just shows the outline of the class (that is why Visual Studio puts [metadata] on top of the 'code file' when hitting F12).

The actual source can be found on 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?

No, they are not the same. ArrayList is a non-generic list implementation, while List<T> is generic, and thus strongly typed.

Regarding the ambiguous name: I think Microsoft is right in their naming of List. ArrayList was a terrible name anyway. It emphasizes the implementation too much. You don't care there is an array behind it: to you it is just a List. Given that the name was available, this was a good option for a name.

这篇关于List的实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆