设置/扩展列表<;T>;长度单位为c#
给定c语言中的设置/扩展列表<;T>;长度单位为c#,c#,list,C#,List,给定c语言中的列表,是否有办法扩展它(在其容量范围内)并将新元素设置为null?我想要像memset一样工作的东西。我不是在找糖,我想要快速代码。我知道在C中,操作可以在每个条目1-3次asm操作中完成 我找到的最佳解决方案是: 然而,这就是c#3.0(为什么要这么做? 列表的主要优点是它可以根据需要增长,那么为什么要向其中添加大量的null或默认元素呢 在这种情况下使用数组不是更好吗?静态IEnumerable GetValues(t值,int计数){ static IEnumerable&l
列表
,是否有办法扩展它(在其容量范围内)并将新元素设置为null
?我想要像memset
一样工作的东西。我不是在找糖,我想要快速代码。我知道在C中,操作可以在每个条目1-3次asm操作中完成
我找到的最佳解决方案是:
然而,这就是c#3.0(为什么要这么做? 列表的主要优点是它可以根据需要增长,那么为什么要向其中添加大量的null或默认元素呢 在这种情况下使用数组不是更好吗?
静态IEnumerable GetValues(t值,int计数){
static IEnumerable<T> GetValues<T>(T value, int count) {
for (int i = 0; i < count; ++i)
yield return value;
}
list.AddRange(GetValues<object>(null, number_of_nulls_to_add));
对于(int i=0;i
这将适用于2.0+最简单的方法可能是创建一个临时数组:
list.AddRange(new T[size - count]);
其中,size
是所需的新大小,count
是列表中项目的计数。但是,对于相对较大的size-count
值,这可能会导致性能不佳,因为它会导致列表多次重新分配。(*
)它还有一个缺点,即分配一个额外的临时数组,这可能是不可接受的,具体取决于您的需求。您可以通过使用以下方法来缓解这两个问题,但需要更明确的代码:
public static class CollectionsUtil
{
public static List<T> EnsureSize<T>(this List<T> list, int size)
{
return EnsureSize(list, size, default(T));
}
public static List<T> EnsureSize<T>(this List<T> list, int size, T value)
{
if (list == null) throw new ArgumentNullException("list");
if (size < 0) throw new ArgumentOutOfRangeException("size");
int count = list.Count;
if (count < size)
{
int capacity = list.Capacity;
if (capacity < size)
list.Capacity = Math.Max(size, capacity * 2);
while (count < size)
{
list.Add(value);
++count;
}
}
return list;
}
}
公共静态类CollectionsUtil
{
公共静态列表(此列表,整数大小)
{
返回(列表、大小、默认值(T));
}
公共静态列表(此列表,int size,T值)
{
如果(list==null)抛出新的ArgumentNullException(“list”);
如果(大小<0)抛出新ArgumentOutOfRangeException(“大小”);
int count=list.count;
如果(计数<大小)
{
int容量=列表容量;
if(容量<尺寸)
list.Capacity=Math.Max(大小,容量*2);
while(计数<大小)
{
列表。添加(值);
++计数;
}
}
退货清单;
}
}
这里唯一的C#3.0是使用“this
”修饰符使它们成为扩展方法。删除修饰符,它将在C#2.0中工作
不幸的是,我从未比较过这两个版本的性能,所以我不知道哪一个更好
哦,您知道可以通过调用来调整数组大小吗?我不知道。:)
更新:(
*
)使用列表。AddRange(数组)
不会导致使用枚举器。通过Reflector进一步查看显示,数组将被强制转换为ICollection
,并且将使用Count
属性,以便只执行一次分配。IIRC数组不能增长(您可以制作更大的副本,但不能增长)列表不是数组。它可以增长。的确,数组可以增长,但是当您想将列表设置为多个(空)元素时,使用列表有什么用呢?或者,我只是误解了这个问题吗?实际上数组可以增长!我不知道,但我注意到Array.Resize()
方法:Array.Resize()不会改变数组的大小。它创建一个新数组并复制元素。注释中的“list.AddRange(new T[size-count]);”看起来不错。我想知道优化器是否可以避免使用枚举器。@BCS,我错了,List.AddRange
的实现实际上针对这种情况进行了优化。我已经更新了我的帖子。这样做的好处是它用所需的值填充了列表。但如果空值或零值足够,只需list.AddRange(新的T[count-list.count]
就足够了。(我想它至少在小数组大小时会表现得更好。)如果这种情况经常发生,我建议使用字典而不是列表
list.AddRange(new T[size - count]);
public static class CollectionsUtil
{
public static List<T> EnsureSize<T>(this List<T> list, int size)
{
return EnsureSize(list, size, default(T));
}
public static List<T> EnsureSize<T>(this List<T> list, int size, T value)
{
if (list == null) throw new ArgumentNullException("list");
if (size < 0) throw new ArgumentOutOfRangeException("size");
int count = list.Count;
if (count < size)
{
int capacity = list.Capacity;
if (capacity < size)
list.Capacity = Math.Max(size, capacity * 2);
while (count < size)
{
list.Add(value);
++count;
}
}
return list;
}
}