Introduction
Like an array, a collection is a series of items of the same type. The particularity with creating an array is that you must know in advance the number of items that will make up the list. There are times when you don't know, you can't know, or you can't predict the number of items of the list. For this reason, you may want to create the list for which you don't specify the maximum number of items but you allow the user of the list to add, locate, or remove items at will. Such a list is referred to as a linked list.
Before creating a list, most of the time you would first decide or define what the list would be made of. As different as collections are, one list can be made of numeric values, such as a list that will be made of numbers. You may want a list that is made of names. Such a list can be created from a class that includes a string member variable. Another type of list can contain complex objects. In some other cases, at the time you are creating the collection class, you may not want to specify its type of items. In this case, you can create the class as generic.
Implementing a Collection
After deciding what each item of the list would be made of, you can create a class that would manage the list. This class would be responsible for all operations that can be performed on the list. If the list will be made of primitive values, you can directly create a field of the desired type. Here is an example:
//code section using System; public class Numbers { public double Number; } public class Exercise { static int Main(string[] args) { Numbers nbrs = new Numbers(); return 0; } }
If the list will be made of objects, you can first create a class that specifies those types of items and then declare its variable in the list class. Here is an example of a simple class that holds a double-precision value:
//code section using System; public class Number { public double Item; } public class Numbers { Number Sample; } public class Exercise { static int Main(string[] args) { Numbers nbrs = new Numbers(); return 0; } }
When creating a list, one of the aspects you should pay attention to is to keep track of the number of items in the list. To do this, you can create a property that holds the number. The value of this property would increase as the items are added to the list and the value would decrease as the items are removed from the list. Here is how this can be done:
//code section using System; public class Number { public double Item; } public class Numbers { int size; Number Sample; public Numbers() { size = 0; } public int Count { get { return size; } } } public class Exercise { static int Main(string[] args) { var nbrs = new Numbers(); Console.WriteLine("Number of Items: {0}", nbrs.Count); return 0; } }
This would produce: Number of Items: 0 Press any key to continue . . .
You can also create a Boolean read-only property that allows you to check whether the list is currently empty or not. When implementing the get accessor of this property, if the size of the list is 0, return true; otherwise, return false. This would be done as follows:
//code section using System; public class Number { public double Item; } public class Numbers { int size; Number Sample; public Numbers() { size = 0; } public int Count { get { return size; } } public bool IsEmpty { get { if (size == 0) return true; return false; } } }