设置/扩展名单< T>长度在C# [英] Set/extend List<T> length in c#

查看:107
本文介绍了设置/扩展名单< T>长度在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个列表< T> 在C#有没有办法把它扩大(力所能及的范围内),并设置了新的元素?我想要的东西,就像一个 memset的。我不是在寻找糖这里,我想快速的代码。我知道,在C的操作能够在像每个条目1-3 ASM OPS来完成。

Given a List<T> in c# is there a way to extend it (within its capacity) and set the new elements to null? I'd like something that works like a memset. I'm not looking for sugar here, I want fast code. I known that in C the operation could be done in something like 1-3 asm ops per entry.

我已经找到了最好的解决办法是的这个

The best solution I've found is this:

list.AddRange(Enumerable.Repeat(null, count-list.Count));



不过那是C#3.0(小于3.0为宜),并可能会产生和评估一个枚举。

however that is c# 3.0 (<3.0 is preferred) and might be generating and evaluating an enumerator.

我目前的代码使用:

while(list.Count < lim) list.Add(null);



所以这是对时间成本的起始点。

so that's the starting point for time cost.

此的动机是,我需要设置即使是老伯爵后第n个元素。

The motivation for this is that I need to set the n'th element even if it is after the old Count.

推荐答案

最简单的方法可能是通过创建一个临时数组:

The simplest way is probably by creating a temporary array:

list.AddRange(new T[size - count]);



其中,尺寸是所需的新的大小,和计数是列表中的项目数。 然而,对于尺寸相对较大的值 - 计数,这个可以有糟糕的表现,因为它可能会导致列表以重新分配多次(<。 code> * ),它也有分配额外的临时数组,其中,根据您的要求,可能是不能接受的缺点。你可以在更明确的代码为代价缓解这两个问题,通过以下方式:

Where size is the required new size, and count is the count of items in the list. However, for relatively large values of size - count, this can have bad performance, since it can cause the list to reallocate multiple times.(*) It also has the disadvantage of allocating an additional temporary array, which, depending on your requirements, may not be acceptable. You could mitigate both issues at the expense of more explicit code, by using the following methods:

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



唯一的C#3.0这里使用的 这个修改,使之扩展方法。取出修改,也将努力在C#2.0。

The only C# 3.0 here is the use of the "this" modifier to make them extension methods. Remove the modifier and it will work in C# 2.0.

不幸的是,我从来没有比较了两个版本的性能,所以我不知道哪一个更好。

Unfortunately, I never compared the performance of the two versions, so I don't know which one is better.

哦,你可知道,你可以通过调用的 Array.Resize< T> ?我不知道。 :)

Oh, and did you know you could resize an array by calling Array.Resize<T>? I didn't know that. :)

更新:结果
* )使用 list.AddRange(阵列)将的的会导致使用一个枚举。进一步看,通过反射显示阵列将被强制转换为的ICollection< T> 计数属性将用于这样的分配只进行一次。

Update:
(*) Using list.AddRange(array) will not cause an enumerator to be used. Looking further through Reflector showed that the array will be casted to ICollection<T>, and the Count property will be used so that allocation is done only once.

这篇关于设置/扩展名单&LT; T&GT;长度在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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