List(IEnumerable),List一个很有意思的用法,在初始化的时候就可以将值和长度全部复制过去:
// name可以是数组、也可以是list或者是其他实现IEnumerable接口的类型,但不能是null
var list = new List<string>(name);
官网相关链接 => link.
官方解释:
**List<T>(IEnumerable<T>)**
Initializes a new instance of the List<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
**Parameters**
collection IEnumerable<T>
The collection whose elements are copied to the new list.
**Exceptions**
ArgumentNullException
collection is null.