Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调整列表大小<;T>;_C#_Arrays_List_Resize - Fatal编程技术网

C# 调整列表大小<;T>;

C# 调整列表大小<;T>;,c#,arrays,list,resize,C#,Arrays,List,Resize,我想调整我的列表的大小。即,根据某些条件更改列表的计数。现在,我正在使用这样一个数组:- private MyModel[] viewPages = GetPagesFromAPI().ToArray(); if (viewPages.Count % 6 == 0) { Array.Resize(ref newViewPages, viewPages.Length / 6); } else { Array.Resize(ref newViewPages, viewPages.L

我想调整我的
列表的大小。即,根据某些条件更改列表的计数。现在,我正在使用这样一个数组:-

private MyModel[] viewPages = GetPagesFromAPI().ToArray();

if (viewPages.Count % 6 == 0) 
{
   Array.Resize(ref newViewPages, viewPages.Length / 6);
} 
else
{
   Array.Resize(ref newViewPages, viewPages.Length / 6 + 1);
}
但是,我认为这不是一种正确的方法,因为这将对我的应用程序造成沉重负担,并可能导致内存问题。有没有一种方法可以使用类似于
List viewPageList
的东西来实现这一点

感谢您的帮助

我想调整我的
列表的大小

您误解了
列表的目的。根据定义,列表是一个自动调整大小的集合,无需手动调整其大小。在添加元素时,它将检查其内部阵列备份存储,并在需要时增加其大小(当前的实现细节将使其备份存储加倍)

以下是如何实现
List.Add

// Adds the given object to the end of this list. The size of the list is
// increased by one. If required, the capacity of the list is doubled
// before adding the new element.
public void Add(T item)
{
     if (_size == _items.Length) EnsureCapacity(_size + 1);
     _items[_size++] = item;
    _version++;
}
EnsureCapacity
将确保备份阵列具有足够的存储空间

我想调整我的
列表的大小

您误解了
列表的目的。根据定义,列表是一个自动调整大小的集合,无需手动调整其大小。在添加元素时,它将检查其内部阵列备份存储,并在需要时增加其大小(当前的实现细节将使其备份存储加倍)

以下是如何实现
List.Add

// Adds the given object to the end of this list. The size of the list is
// increased by one. If required, the capacity of the list is doubled
// before adding the new element.
public void Add(T item)
{
     if (_size == _items.Length) EnsureCapacity(_size + 1);
     _items[_size++] = item;
    _version++;
}

EnsureCapacity
将确保备份阵列具有足够的存储空间。

您不使用
列表的任何原因可能您正在寻找类似的内容:您不使用
列表的任何原因可能您正在寻找类似的内容: