Home

Introduction to Collections

 

Array-Based Lists

 

Introduction

A collection, also called a list, is many items of the same kind grouped into one entity. The collection can be based on simple numbers, basic symbols, dates, strings, times. The collection can also be made of objects that each is made of internal values. This means that a collection can be made of such objects as houses, people, cars, countries, electronic devices. The primary rule is all items included in a list must be described using the same characteristics. Based on this, if the collection is made of cars, it should contain only cars, not cars and countries.

There are various types of Lists or collections. An array-based list is one whose number of items is known in advance. For example, an array-based collection of 10 cars contains 10 cars, may-be less but not more.

 

Setting Up a Collection

To use a collection as an entity, you can create a class for it. This class would be used to add items in the collection, to remove items in the list, or to perform other necessary operations. You can start from a simple class as follows:

class Collection
{
    public Collection()
    {
    }
}

After creating the class for a collection, the collection becomes an object and its class can be used like any other. This means that, before using the collection, you can declare a variable for it. Here is an example:

using System;

class Collection
{
    public Collection()
    {
    }
}

class Program
{
    static int Main(string[] args)
    {
        Collection list = new Collection();

        return 0;
    }
}

The Number of Items in a Collection

For an array-based list, because you must specify the number of items that the list will contain, you can declare an array as a member variable. Here is an example:

class Collection
{
    // This collection will be a list of decimal numbers
    private double[] Item = new double[20];

    // Our default constructor, used to initialize the collection
    public Collection()
    {
    }
}

Although you can initialize an array in a C# class, remember that you can also use a constructor to initialize it. Here is an example:

 

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
    }
    #endregion
}

A primary accessory you need when using a list is a count of the number of items in the list when they are added or deleted. This accessory is primarily a private field as a simple natural number. When the class is declared, this member variable should be set to 0 to indicate that the list is primarily empty:

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
	this.size = 0;
    }
    #endregion
}

Since the size member variable was declared private, if you plan to get the count of items in the list from outside the class, you can provide a property to take care of this. This can simply be done as follows:

using System;

class Collection
{
    public int MaxCount = 20;

    // This collection will be a list of decimal numbers
    private double[] Item;
    // This is the size of the collection
    private int size;

    #region This section is used to set up the collection
    // Our default constructor, used to initialize the collection
    public Collection()
    {
        this.Item = new double[MaxCount];
        this.size = 0;
    }

    // This represents the number of items in the collection
    public int Count
    {
        get { return this.size; }
    }
    #endregion
}

class Program
{
    static int Main(string[] args)
    {
        Collection list = new Collection();

        Console.WriteLine("Number of Items: {0}", list.Count);
        return 0;
    }
}

This would produce:

Number of Items: 0
Press any key to continue . . .
 

Home Copyright © 2006-2007 FunctionX, Inc. Next