C# 什么';清单的实施情况如何?
我读了这段代码:C# 什么';清单的实施情况如何?,c#,.net,generics,generic-list,C#,.net,Generics,Generic List,我读了这段代码: List<long> userIdList = new List<long>(); 它不是接口或抽象,但它没有函数体(对于该类中的任何方法)。我知道ArrayList和LinkedList,但是对于List,我不知道它的实现 我的问题: 列表的实现在哪里 如果List等于ArrayList或其他什么,为什么.net会允许两个函数相等但名称不同的类?如果List不等于.NET中的任何其他类,那么为什么要给它起这样一个模棱两可的名字呢 : List类是Ar
List<long> userIdList = new List<long>();
它不是接口或抽象,但它没有函数体(对于该类中的任何方法)。我知道ArrayList
和LinkedList
,但是对于List
,我不知道它的实现
我的问题:
列表的实现在哪里
List
等于ArrayList
或其他什么,为什么.net会允许两个函数相等但名称不同的类?如果List
不等于.NET中的任何其他类,那么为什么要给它起这样一个模棱两可的名字呢List
不等于ArrayList
。列表
是强类型的,而数组列表
使用对象
作为其内部集合,因此不是强类型的
前者是在.NET中引入的
我不认为
列表
有任何模棱两可之处。它是一个列表,可以包含任何参数作为其内部存储器,例如列表
、列表
或列表
无法从Visual Studio中显示列表的实现,因为它没有源代码。它只是显示类的概要(这就是为什么VisualStudio在点击F12时将[元数据]放在“代码文件”的顶部)
可在上找到实际来源
若List等于ArrayList或其他什么,为什么.net会允许两个函数相等但名称不同的类?若列表不等于.NET中的任何其他类,那个么为什么给它起这么一个模棱两可的名字呢
不,它们不一样ArrayList
是非泛型列表实现,而list
是,因此是强类型的
关于这个模棱两可的名字:我认为微软对列表的命名是正确的ArrayList
无论如何都是个糟糕的名字。它过于强调执行。你不在乎它背后有一个数组:对你来说,它只是一个列表
。鉴于名称可用,这是一个很好的名称选择。源代码在这里:-摘自你为什么说List
是一个模棱两可的名称?@Enigmativity可能我已经习惯了Java。。在Java中,List是抽象的
,我们应该声明List的一个具体实现来使用它。@Sayakiss Java似乎将它们称为ArrayList
。这与列表
相同。
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);
...