Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
C#
public int Capacity
        {
            get
            {
                return 5;
            }
            set
            {
            }
        }

Is it possible to increase capacity by adding new members into collection?
Posted

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[^]
 
Share this answer
 
Comments
Inimicos 24-Nov-14 5:38am    
Yes i know,but i got error,out of range,without capacity property.
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:
C#
class somelist : CollectionBase
    {
        public int Capacity
        {
            get
            {
                return List.Count();
            }

would do the work.

I do not recommend to use arrays. Use List<T> instead. See: List<T> Class[^]
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900