如何自动增加收集库的容量? [英] How to increase capacity of collectionbase automatticly?

查看:75
本文介绍了如何自动增加收集库的容量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.



http://msdn.microsoft.com/en-us/library/system.collections .collectionbase.capacity(v = vs.110).aspx [ ^ ]


我不会说你做错了,但是你需要重新思考一下。请阅读:

演练:创建你自己的收藏班 [ ^ ]



您的类继承自CollectionBase,因此:

I won't say you're doing it wrong, but you need to re-think it. Please, read this:
Walkthrough: Creating Your Own Collection Class[^]

Your class inherits from CollectionBase, so:
class somelist : CollectionBase
    {
        public int Capacity
        {
            get
            {
                return List.Count();
            }



可以完成这项工作。



我不建议使用数组。使用列表< T>代替。参见: List< T>类 [ ^ ]


为什么您认为添加属性以返回固定数字会影响基础类中项目数量的限制?你所要做的就是从编译器那里得到一个警告,说你正在隐藏这个继承的成员。它不会以任何方式影响基础类。



集合的容量不限制它可以包含多少元素:它是一个警告超过此值会导致容量增加时出现小延迟。



例如,集合可能使用数组实现,并以容量8开始:当你添加第9个项目时,它会分配一个包含16个项目的新数组,并在添加第9个项目之前复制现有的8个项目。当你添加第17个时,它会做同样的但是这次分配32个空格



如果你想限制你的收藏到特定的大小,你需要覆盖Add方法并决定当用户试图超出限制时该怎么做。
Why do you think that adding a property to return a fixed number is going to affect the limit on number of items in the underlying class? All you will do is get a warning from the compiler to say "you are hiding this inherited member". It doesn't affect the underlying class in any way.

The Capacity of a collection is not a limit on how many elements it can contain: it is a "warning" that exceeding this will cause a small delay while the capacity is increased.

For example, a collection might be implemented with an array and start with a capacity of 8: when you add the 9th item, it allocates a new array of 16 items, and copies the existing 8 over before adding the 9th. When you add the 17th, it does the same but this time allocates 32 "spaces"

If you want to restrict your collection to a specific size, you will need to override the Add method(s) and decide what to do when the user tries to exceed your limit.


这篇关于如何自动增加收集库的容量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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