本文介绍了如何自动增加收集库的容量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Collectionsexample
{
class Program
{
static void Main(string[] args)
{
car[] banchcars = new car[5];
banchcars[0] = new car();
banchcars[0].count = 1;
banchcars[0].name = "bmw";
banchcars[0].type = "hatchback";
ArrayList banchofbanchescar = new ArrayList();
passengers[] banchpassengers = new passengers[5];
banchofbanchescar.Add(banchpassengers);
banchofbanchescar.Add(banchcars);
ArrayList banchofbanchofcars = new ArrayList();
somelist someexampleofcollectionbase = new somelist();
car Mersedes = new car();
Mersedes.count = 1;
Mersedes.name = "Mersedes";
Mersedes.type = "sedan";
someexampleofcollectionbase.newcar(Mersedes);
car Lincoln = new car();
Lincoln.count = 1;
Lincoln.name = "Lincoln";
Lincoln.type = "sedan";
someexampleofcollectionbase.newcar(Lincoln);
someexampleofcollectionbase.newcar(Mersedes);
Console.WriteLine(someexampleofcollectionbase[0].name);
Console.WriteLine(someexampleofcollectionbase[1].name);
Console.Read();
}
}
class car
{
public int count;
public string name;
public string type;
}
class passengers
{
public string name;
public int sex;
public int weight;
}
class somelist : CollectionBase
{
public int Capacity
{
get
{
return 5;
}
set
{
}
}
public string showinfo(car infoaboutcar)
{
return infoaboutcar.name;
}
public void newcar(car Newcar)
{
List.Add(Newcar);
}
public car this[int a]
{
get
{
return (car)List[a];
}
set
{
List[a] = value;
}
}
}
}
我写了一些代码我通过属性手动更改了容量
I wrote some code i changed capacity manually by property
public int Capacity
{
get
{
return 5;
}
set
{
}
}
是否可以通过在集合中添加新成员来增加容量?
Is it possible to increase capacity by adding new members into collection?
推荐答案
MSDN:
...通过在复制旧元素和添加新元素之前重新分配内部数组来自动增加容量。
... the capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.
[]
class somelist : CollectionBase
{
public int Capacity
{
get
{
return List.Count();
}
可以完成这项工作。
我不建议使用数组。使用列表< T>代替。参见: []
这篇关于如何自动增加收集库的容量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!