Say*_*iss 5 .net c# generics generic-list
我读了这段代码:
List<long> userIdList = new List<long>();
Run Code Online (Sandbox Code Playgroud)
但我跳到List(in System.Collections.Generic)的定义(使用VS2012 ),我发现:
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);
...
Run Code Online (Sandbox Code Playgroud)
它不是Interface或Abstract,但它没有函数体(对于该类中的任何方法).我知道ArrayList和LinkedList,但List我不知道它的实现思路.
我的问题:
List?List等于ArrayList或什么,为什么.net将允许两个等于函数但名称不同的类?如果List不等于.NET中的任何其他类,那么为什么要给它这么模糊的名字?List类是ArrayList类的通用等价物.它通过使用一个数组来实现IList泛型接口,该数组的大小根据需要动态增加.
所以,我认为这是一个坏名字......
Pat*_*man 15
List<T>无法从Visual Studio中显示实现,因为它没有源代码.它只显示了类的轮廓(这就是为什么Visual Studio 在按下F12时将[元数据]放在'代码文件'之上).
实际来源可以在referencesource.microsoft.com上找到.
如果List等于ArrayList或其他什么,为什么.net将允许两个等于函数但名称不同的类?如果List不等于.NET中的任何其他类,那么为什么要给它这么模糊的名字?
不,他们不一样.ArrayList是一个非通用的列表实现,虽然List<T> 是 通用的,因此是强类型的.
关于模糊的名称:我认为微软的命名是正确的List.ArrayList反正是一个可怕的名字.它强调实施过多.你不在乎它后面有一个数组:对你来说它只是一个List.鉴于该名称可用,这是一个名称的好选择.
List的实现在哪里?
您所看到的是VS允许您查看的内容,它实际上不是代码,而是每个方法文档的简要摘要.如果您需要代码,可在此处获取源代码
List和ArrayList是否相等?如果List不等于.NET中的任何其他类,那么为什么要给它这么模糊的名字?
List<T>不相等ArrayList.一个List<T>是强类型,同时ArrayList使用object,因为它的内部集合,因此不是强类型.
在.NET中引入泛型时,前者变得生动起来.
我认为没有任何含糊之处List<T>.它是一个列表,该列表可以包含任何参数,因为它的内部存储设备,例如List<int>,List<string>或List<Foo>.