Introduction to List-Based Classes

Overview

To support the creation of any kinds of list, the .NET Framework provides a collection class named ArrayList and a generic collection class named List. The ArrayList class is defined in the System.Collections namespace while the generic List class is a member of the System.Collections.Generic namespace. The ArrayList class starts as follows:

public class ArrayList : IList,
			 ICollection,
			 IEnumerable,
			 ICloneable

The generic List<T> class starts as follows:

public class List<T> : IList<T>,
			     ICollection<T>,
			     IEnumerable<T>,
			     IList,
			     ICollection,
			     IEnumerable

Therefore, to use one of these classes in C# code, you can first include its namespace in the top section of the document. Otherwise, in your code, to create an ArrayList collection in your code, you can use the class as System.Collections.ArrayList. As you might have noticed, if you create a Console App application, the using System.Collections.Generic; line is automatically added, in which case you can directly use the List<T> class in your code.

The ArrayList class implements the IList, the ICollection, and the IEnumerable interfaces. The List class implements the generic IList<>, the generic ICollection<>, the generic IEnumerable<>, the IList, the ICollection, and the IEnumerable interfaces.

You can use either the ArrayList or the generic List classes to create and manage values for a list. Here are examples of declaring variables of these classes:

using System.Collections;
using System;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        List<string> atoms = new List<string>();
        DateTime dtMade = new DateTime();
        ArrayList molecules = new ArrayList();

        return 0;
    }
}

Instead of primitive types, the values of an ArrayList or of a List<> collection can be objects of a class. For a List<> collection, you must specify the parameter type. Here is an example:

using System;
using System.Collections.Generic;

[Serializable]
public class Employee
{
    public string EmployeeNumber { get; set; }
    public string FirstName      { get; set; }
    public string LastName       { get; set; }
    public string City           { get; set; }
    public string State          { get; set; }
    public string ZIPCode        { get; set; }
    public string MaritalStatus  { get; set; }
    public int    Exemptions     { get; set; }
    public double HourlySalary   { get; set; }
    public string FilingStatus   { get; set; }
}

public class PayrollPreparation
{
    public static int Main()
    {
        List<Employee> employees = new List<Employee>();

        return 1000;
    }
}

Besides the ability to create a list, both the ArrayList and the List<T> classes have the built-in mechanism for serialization.

The default constructor of each class allows you to create an empty list before adding values to it. If you already have an ICollection-based list, that is, a list created from a class that implements the ICollection interface, you can initialize your ArrayList collection with it. To support this, the ArrayList class is equipped with the following constructor:

public ArrayList (System.Collections.ICollection c);

Practical LearningPractical Learning: Introducing Lists

  1. Start Microsoft Visual Studio
  2. On the main menu, click File -> New -> Project...
  3. In the middle list, click Console App (.NET Framework) and set the project Name to JustInPay1
  4. Click OK
  5. In the Solution Explorer, right-click JustInPay1 -> Add -> Class...
  6. Type Employee as the class/file Name
  7. Click Add
  8. Change the class as follows:
    using System;
    
    namespace JustInPay1
    {
        [Serializable]
        public class Employee
        {
            public string EmployeeNumber { get; set; }
            public string FirstName      { get; set; }
            public string LastName       { get; set; }
            public string Address        { get; set; }
            public string City           { get; set; }
            public string County         { get; set; }
            public string State          { get; set; }
            public string ZIPCode        { get; set; }
            public string MaritalStatus  { get; set; }
            public int Exemptions        { get; set; }
            public double HourlySalary   { get; set; }
            public string FilingStatus   { get; set; }
    
            public override bool Equals(object obj)
            {
                Employee empl = (Employee)obj;
    
                if (empl.EmployeeNumber == EmployeeNumber)
                    return true;
    
                return false;
            }
    
            public override int GetHashCode()
            {
                return base.GetHashCode();
            }
        }
    }
  9. In the Solution Explorer, right-click JustInPay1 -> Add -> Class...
  10. Type TimeSheet as the file Name
  11. Click Add
  12. Change the class as follows:
    using System;
    
    namespace JustInPay1
    {
        [Serializable]
        public class TimeSheet
        {
            public int      TimeSheetNumber { get; set; }
            public string   EmployeeNumber  { get; set; }
            public DateTime StartDate       { get; set; }
            public double   Week1Monday     { get; set; }
            public double   Week1Tuesday    { get; set; }
            public double   Week1Wednesday  { get; set; }
            public double   Week1Thursday   { get; set; }
            public double   Week1Friday     { get; set; }
            public double   Week1Saturday   { get; set; }
            public double   Week1Sunday     { get; set; }
            public double   Week2Monday     { get; set; }
            public double   Week2Tuesday    { get; set; }
            public double   Week2Wednesday  { get; set; }
            public double   Week2Thursday   { get; set; }
            public double   Week2Friday     { get; set; }
            public double   Week2Saturday   { get; set; }
            public double   Week2Sunday     { get; set; }
    
            public override bool Equals(object obj)
            {
                TimeSheet ts = (TimeSheet)obj;
    
                if (ts.TimeSheetNumber == TimeSheetNumber)
                    return true;
    
                return false;
            }
    
            public override int GetHashCode()
            {
                return base.GetHashCode();
            }
        }
    }
  13. In the Solution Explorer, right-click JustInPay1 -> Add -> Class...
  14. Type Payroll as the file Name
  15. Click Add
  16. Change the class as follows:
    using System;
    
    namespace JustInPay1
    {
        [Serializable]
        public class Payroll
        {
            public int    PayrollNumber            { get; set; }
            public string EmployeeNumber           { get; set; }
            public string EmployeeFirstName        { get; set; }
            public string EmployeeLastName         { get; set; }
            public string EmployeeAddress          { get; set; }
            public string EmployeeCity             { get; set; }
            public string EmployeeCounty           { get; set; }
            public string EmployeeState            { get; set; }
            public string EmployeeZIPCode          { get; set; }
            public string EmployeeMaritalStatus    { get; set; }
            public int    EmployeeExemptions       { get; set; }
            public double EmployeeHourlySalary     { get; set; }
            public int    EmployeeFilingStatus     { get; set; }
    
            public int    TimeSheetNumber          { get; set; }
            public string TimeSheetStartDate       { get; set; }
            public double TimeSheetWeek1Monday     { get; set; }
            public double TimeSheetWeek1Tuesday    { get; set; }
            public double TimeSheetWeek1Wednesday  { get; set; }
            public double TimeSheetWeek1Thursday   { get; set; }
            public double TimeSheetWeek1Friday     { get; set; }
            public double TimeSheetWeek1Saturday   { get; set; }
            public double TimeSheetWeek1Sunday     { get; set; }
            public double TimeSheetWeek2Monday     { get; set; }
            public double TimeSheetWeek2Tuesday    { get; set; }
            public double TimeSheetWeek2Wednesday  { get; set; }
            public double TimeSheetWeek2Thursday   { get; set; }
            public double TimeSheetWeek2Friday     { get; set; }
            public double TimeSheetWeek2Saturday   { get; set; }
            public double TimeSheetWeek2Sunday     { get; set; }
    
            public double RegularTime              { get; set; }
            public double Overtime                 { get; set; }
            public double RegularPay               { get; set; }
            public double OvertimePay              { get; set; }
            public double GrossSalary              { get; set; }
    
            public double TaxableGrossWagesCurrent { get; set; }
            public double AllowancesCurrent        { get; set; }
            public double FederalIncomeTaxCurrent  { get; set; }
            public double SocialSecurityTaxCurrent { get; set; }
            public double MedicareTaxCurrent       { get; set; }
            public double StateIncomeTaxCurrent    { get; set; }
    
            public double TaxableGrossWagesYTD     { get; set; }
            public double AllowancesYTD            { get; set; }
            public double FederalIncomeTaxYTD      { get; set; }
            public double SocialSecurityTaxYTD     { get; set; }
            public double MedicareTaxYTD           { get; set; }
            public double StateIncomeTaxYTD        { get; set; }
    
            public override bool Equals(object obj)
            {
                Payroll pr = (Payroll)obj;
    
                if (pr.PayrollNumber == PayrollNumber)
                    return true;
    
                return false;
            }
    
            public override int GetHashCode()
            {
                return base.GetHashCode();
            }
        }
    }
  17. In the Solution Explorer, right-click JustInPay1 -> Add -> Class...
  18. Type WorkDay as the file Name
  19. Click Add
  20. Create the class as follows:
    namespace JustInPay1
    {
        public class WorkDay
        {
            public WorkDay(double time, double salary)
            {
                HourSalary = salary;
                TimeWorked = time;                            
            }
    
            public double HourSalary { get; set; }
            public double TimeWorked { get; set; }
    
            public double OvertimeRate => HourSalary * 1.50;
    
            public double RegularTime
            {
                get
                {
                    if (TimeWorked <= 8.00)
                        return TimeWorked;
                    else
                        return 8.00;
                }
            }
    
            public double Overtime
            {
                get
                {   
                    if (TimeWorked <= 8.00)
                        return 0.00;
                    else
                        return TimeWorked - 8.00;
                }
            }
    
            public double RegularPay  => RegularTime * HourSalary;
            public double OvertimePay => OvertimeRate * Overtime;
            public double NetPay      => RegularPay + OvertimePay;
        }
    }

The Capacity of a List

After declaring an ArrayList or a List<T> variable, it is empty. As objects are added to it, the list grows. The list can grow tremendously as you wish. The number of items of the list is managed through the memory it occupies and this memory grows as needed. The number of items that the memory allocated is currently using is represented by the Capacity property:

ArrayList: public virtual int Capacity { get; set; }
List<T>:   public int Capacity { get; set; }

The capacity of a list will usually be the least of your concerns. If for some reason, you want to intervene and control the number of items that your list can contain, you can manipulate the Capacity property. For example, you can assign it a constant to set the maximum value that the list can contain. Instead of specifying the capacity after the list has been created, when declaring the list variable, you can specify its maximum capacity. To support this, both the ArrayList and the List classes are equipped with an additional constructor as follows:

public ArrayList(int capacity);
public List(int capacity);

You will hardly have any reason to use the Capacity property.

The Number of Items in a Collection

To provide the number of items in a collection, both the ArrayList and the List classes are equipped with a property named Count, which is an integer.

The Capacity and the Count properties have this in common: the value of each increases as the list grows and the same value decreases if the list shrinks. On the other hand, Capacity is a read/write property. This means that you can assign a value to the capacity to fix the number of items that the list can contain. You can also get the value of the Capacity. The Count property is read-only because the counting of the current number of items is performed without your intervention.

A Read-Only List

A collection is said to be read-only if it doesn't allow the addition of items. To let you produce a read-only collection, the ArrayList provides an overloaded method named ReadOnly. The syntaxes of the two versions are:

public static ArrayList ReadOnly(ArrayList list)
public static IList ReadOnly(IList list)

Some operations cannot be performed on a read-only list. To perform such operations, you can first find out whether an ArrayList list is read-only. This is done by checking its IsReadOnly property.

Adding Values or Objects to a Collection

Adding an Item

The primary operation performed on a list is to add a value or an object to it. To support this, both the ArrayList and the List classes are equipped with an Add() method. The syntax of the System.Collections.ArrayList.Add() method is:

public virtual int Add(object value);

The syntax of the System.Collections.Generic.List.Add() method is:

public void Add(T value);

The argument of the method is the value or object to add to the collection. If the method succeeds with the addition, it returns the position where the value or object was added in the list. Here are example for an ArrayList variable:

using System.Collections;

public class Exercise
{
    public static int Main()
    {
        ArrayList molecules = new ArrayList();

        molecules.Add("Ozone");
        molecules.Add("Oxygen");
        molecules.Add("Helium");
        molecules.Add("Sulfuric Acid");
        molecules.Add("Carbon Dioxide");
    }
}

If the method fails to add the value and if you are using an ArrayList class, the compiler would throw an exception. The error could result from the list being read-only or being full.

ApplicationPractical Learning: Adding an Item to a Collection

  1. In the Solution Explorer, right-click JustInPay1 -> Add -> Class...
  2. Type StaffMembers as the name of the class
  3. Click Add
  4. Change the document as follows:
    using System.IO;
    using static System.Console;
    using System.Collections.Generic;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace JustInPay1
    {
        public class StaffMembers
        {
            public void HireEmployee()
            {
                int exemptions = 0;
                double salary = 0.00;
                string emplNbr = string.Empty;
                string fName = string.Empty, lName = string.Empty;
                string address = string.Empty, city = string.Empty;
                string county = string.Empty, state = string.Empty;
                string zip = string.Empty, marital = string.Empty, filing = string.Empty;
    
                FileStream fsEmployees = null;
                List<Employee> employees = new List<Employee>();
                BinaryFormatter bfEmployees = new BinaryFormatter();
                string strEmployeesFile = @"C:\Payroll Management\Employees.mpl";
    
                // In order to create an Employee object, make sure the user provides at least an employee number.
                if (!string.IsNullOrEmpty("EmployeeNumber"))
                {
                    // If a file for employees records was proviously created, open it
                    if (File.Exists(strEmployeesFile))
                    {
                        using (fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            /* After opening the file, get the list of records from it
                             * and store in the empty List<> collection we started. */
                            employees = (List<Employee>)bfEmployees.Deserialize(fsEmployees);
                        }
                    }
    
                    Title = "New Employee";
    
                    WriteLine("New Employee");
                    WriteLine("-------------------------------------------");
                    Write("Employee #:          ");
                    emplNbr = ReadLine();
                    Write("First Name:          ");
                    fName = ReadLine();
                    Write("Last Name:           ");
                    lName = ReadLine();
                    Write("Address:             ");
                    address = ReadLine();
                    Write("City:                ");
                    city = ReadLine();
                    Write("County:              ");
                    county = ReadLine();
                    Write("State:               ");
                    state = ReadLine();
                    Write("ZIP-Code:            ");
                    zip = ReadLine();
                    Write("Hourly Salary:       ");
                    salary = double.Parse(ReadLine());
                    Write("Exemptions:          ");
                    exemptions = int.Parse(ReadLine());
                    WriteLine("Maritals Status: ");
                    WriteLine(" 1 - Single");
                    WriteLine(" 2 - Married");
                    Write("Type your selection: ");
                    string answerMaritalStatus = ReadLine();
                    WriteLine("Filing Status:   ");
                    WriteLine(" 1 - Head of Household");
                    WriteLine(" 2 - Married Filing Jointly");
                    Write("Type your choice:    ");
                    string answerFilingStatus = ReadLine();
    
                    if (answerMaritalStatus.ToLower().Equals("1"))
                        marital = "Single";
                    else if (answerMaritalStatus.ToLower().Equals("1"))
                        marital = "Married";
                    else
                        marital = "Unknown";
    
                    if (answerFilingStatus.ToLower().Equals("1"))
                        filing = "Head of Household";
                    else if (answerFilingStatus.ToLower().Equals("1"))
                        filing = "Married Filing Jointly";
                    else
                        filing = "Unknown";
    
                    // Create an Employee object using the data from the form
                    Employee empl = new Employee()
                    {
                        EmployeeNumber = emplNbr,
                        FirstName = fName,
                        LastName = lName,
                        Address = address,
                        City = city,
                        County = county,
                        State = state,
                        ZIPCode = zip,
                        MaritalStatus = marital,
                        Exemptions = exemptions,
                        HourlySalary = salary,
                        FilingStatus = filing
                    };
    
                    // Add the new employee to the existing list of employees
                    employees.Add(empl);
    
                    // Save the list of employees in the appropriate file
                    using (fsEmployees = new FileStream(strEmployeesFile, FileMode.Create, FileAccess.Write, FileShare.Write))
                    {
                        bfEmployees.Serialize(fsEmployees, employees);
                    }
                }
            }
        }
    }
    
  5. In the Solution Explorer, right-click Program.cs and click Rename
  6. Type PayrollPreparation to get PayrollPreparation.cs, and press Enter
  7. Read the text in the message box and click Yes
  8. Click the PayrollPreparation.cs tab and change its document as follows:
    using System.IO;
    using static System.Console;
    
    namespace JustInPay1
    {
        public class PayrollPreparation
        {
            public static int Main(string[] args)
            {
                Directory.CreateDirectory(@"C:\Payroll Management");
    
                WriteLine("Just-in-Pay - Payroll Preparation Services");
                WriteLine("-------------------------------------------------------------------------------");
                WriteLine("Just-In-Pay is a private company that creates and manages payrol on " +
                          "behalf of all types of entities, including individuals, small " +
                          "businesses, enterprises, as well as government agencies. The " +
                          "management involves all aspects of time sheets (or time cards). " +
                          "The Just-In-Pay company spares the hassle of tax laws and " +
                          "related regulations so its clients can concentrate on their most " +
                          "important jobs: taking care of their employees and delivering the " +
                          "best services and products they can to their customers.");
                WriteLine("===============================================================================");
                Write("Press any key to display the main menu...");
    
                ReadKey();
                Clear();
                StaffMembers personnel = new StaffMembers();
    
                personnel.HireEmployee();
                return 0;
            }
        }
    }
  9. To execute the project, on the main menu, Debug -> Start Without Debugging:
    Just-in-Pay - Payroll Preparation Services
    -------------------------------------------------------------------------------
    Just-In-Pay is a private company that creates and manages payrol on behalf of al
    l types of entities, including individuals, small businesses, enterprises, as we
    ll as government agencies. The management involves all aspects of time sheets (o
    r time cards). The Just-In-Pay company spares the hassle of tax laws and related
     regulations so its clients can concentrate on their most important jobs: taking
     care of their employees and delivering the best services and products they can
    to their customers.
    ===============================================================================
    Press any key to display the main menu...
  10. Press any key to display a new window:
    New Employee
    -------------------------------------------
    Employee #:          
  11. From the following table, type the values when prompted and press Enter after each. Execute the application again to create each new record:


      1 2 3 4
    Employee # 941148 927048 606384 952748
    First Name Catherine Henry Herbert David
    Last Name Watts Meuer Gibson Evans
    Address 12004 Harrington Ave 802 Wheeler Street 10324 Marina Ave 5102 Piedmont Rd
    City Baltimore York College Park Silver Spring
    County Baltimore York Prince George Montgomery
    State MD PA MD MD
    ZIP Code 21206 17401 20742 20910
    Marital Status Single

    Single

    Single

    Single
    Exemptions 0 3 1 2
    Hourly Salary 26.15 18.95 22.25 17.25
    Filing Status Head of Household Head of Household Head of Household Head of Household
  12. Press any key to close the window and return to your programming environment

Adding a Range of Items

Instead of adding one value at a time, you can first create a list of values and add that whole list at once. To support this operation, both the ArrayList and the List classes are equipped with a method named AddRange.

The syntax of the ArrayList.AddRange() method is:

public virtual void AddRange(ICollection collection);

The syntax of the List<>.AddRange() method is:

public void AddRange(IEnumerable<T> collection);

The ArrayList.AddRange() method takes as argument a list created from a class that implements the ICollection interface. The List<>.AddRange() method takes as argument a list created from a class that implements the generic IEnumerable interface.

Practical LearningPractical Learning: Adding a Range of Items

  1. In the Solution Explorer, right-click JustInPay1 -> Add -> Class...
  2. Type Payrolls to get PayrollController
  3. Press Enter
  4. Change the document as follows:
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace JustInPay1
    {
        public class Payrolls
        {
            public void CreateTimeSheets()
            {
                List<TimeSheet> timeSheets = new List<TimeSheet>();
                BinaryFormatter bfTimeSheets = new BinaryFormatter();
                string strTimeSheetsFile = @"C:\Payroll Management\TimeSheets.tss";
    
                List<TimeSheet> totalWork = new List<TimeSheet>();
    
                totalWork.Add(new TimeSheet() { TimeSheetNumber = 100001, EmployeeNumber = "606384", StartDate = new DateTime(2018, 1, 1), Week1Monday = 0, Week1Tuesday = 0, Week1Wednesday = 0.00, Week1Thursday = 0.00, Week1Friday = 0.00, Week1Saturday = 8, Week1Sunday = 8, Week2Monday = 0.00, Week2Tuesday = 0, Week2Wednesday = 0.00, Week2Thursday = 0, Week2Friday = 0, Week2Saturday = 8, Week2Sunday = 8.00 });
                totalWork.Add(new TimeSheet() { TimeSheetNumber = 100002, EmployeeNumber = "952748", StartDate = new DateTime(2018, 1, 1), Week1Monday = 8, Week1Tuesday = 8, Week1Wednesday = 8.00, Week1Thursday = 8.00, Week1Friday = 8.00, Week1Saturday = 0, Week1Sunday = 0, Week2Monday = 8.00, Week2Tuesday = 8, Week2Wednesday = 8.00, Week2Thursday = 8, Week2Friday = 8, Week2Saturday = 0, Week2Sunday = 0.00 });
                totalWork.Add(new TimeSheet() { TimeSheetNumber = 100003, EmployeeNumber = "941148", StartDate = new DateTime(2018, 1, 1), Week1Monday = 9, Week1Tuesday = 10, Week1Wednesday = 8.50, Week1Thursday = 9.50, Week1Friday = 10.50, Week1Saturday = 12, Week1Sunday = 12, Week2Monday = 8.50, Week2Tuesday = 9, Week2Wednesday = 9.50, Week2Thursday = 8, Week2Friday = 10, Week2Saturday = 10, Week2Sunday = 8.50 });
                totalWork.Add(new TimeSheet() { TimeSheetNumber = 100004, EmployeeNumber = "927048", StartDate = new DateTime(2018, 1, 1), Week1Monday = 8, Week1Tuesday = 8, Week1Wednesday = 8.00, Week1Thursday = 8.00, Week1Friday = 8.00, Week1Saturday = 0, Week1Sunday = 0, Week2Monday = 8.00, Week2Tuesday = 8, Week2Wednesday = 8.00, Week2Thursday = 8, Week2Friday = 8, Week2Saturday = 0, Week2Sunday = 0.00 });
    
                timeSheets.AddRange(totalWork);
    
                using (FileStream fsTimeSheets = new FileStream(strTimeSheetsFile, FileMode.Create, FileAccess.Write, FileShare.Write))
                {
                    bfTimeSheets.Serialize(fsTimeSheets, timeSheets);
                }
            }
        }
    }
  5. Click the PayrollPreparation.cs tab and change its document as follows:
    using System.IO;
    using static System.Console;
    
    namespace JustInPay1
    {
        public class PayrollPreparation
        {
            public static int Main(string[] args)
            {
                Directory.CreateDirectory(@"C:\Payroll Management");
    
                WriteLine("Just-in-Pay - Payroll Preparation Services");
                WriteLine("-------------------------------------------------------------------------------");
                WriteLine("Just-In-Pay is a private company that creates and manages payrol on " +
                          "behalf of all types of entities, including individuals, small " +
                          "businesses, enterprises, as well as government agencies. The " +
                          "management involves all aspects of time sheets (or time cards). " +
                          "The Just-In-Pay company spares the hassle of tax laws and " +
                          "related regulations so its clients can concentrate on their most " +
                          "important jobs: taking care of their employees and delivering the " +
                          "best services and products they can to their customers.");
                WriteLine("===============================================================================");
                Write("Press any key to display the main menu...");
    
                ReadKey();
                Clear();
                Payrolls pays = new Payrolls();
    
                pays.CreateTimeSheets();
                return 0;
            }
        }
    }
  6. To test the application, on the main menu, click Debug -> Start Debugging
  7. Press any key to create the time sheets
  8. Press any key to close the window and return to your programming environment

Getting a Value or Object from a Collection

Introduction

A computer language like C# has built-in means to look for an item in a collection. This can be done by first using a loop to visit each member of a collection, then using a conditional statement to check the value of an item or compare it to a condition.

Getting to an Item Using an Indexer

To give you access to each member of their list, both the ArrayList and the List classes are equipped with the default Item property. The Item property is an indexer. The first value of the list has an index of 0. The second has an index of 1, and so on.

To get a single value based on its position, you can apply the square brackets of arrays to the variable. Here is an example:

using System.Collections;
using static System.Console;

public class Exercise
{
    public static int Main()
    {
        ArrayList molecules = new ArrayList();

        Title = "Chemistry";
        WriteLine("Chemistry");
        WriteLine("---------------------");

        molecules.Add("Ozone");
        molecules.Add("Oxygen");
        molecules.Add("Helium");
        molecules.Add("Sulfuric Acid");
        molecules.Add("Carbon Dioxide");

        WriteLine("Molecule: " + molecules[1]);
        WriteLine("===============================");
        return 0;
    }
}

This would produce:

Chemistry
---------------------
Molecule: Oxygen
===============================
Press any key to continue . . .

Based on this, you can use a loop (while, do...while, or for) to visit each item through its index.

An issue to keep in mind is that the ArrayList[] indexer returns an Object value. Therefore, you may have to cast this value to your type of value to get it right.

Iterating Through a Collection

Besides using the index to access a value from the list, the ArrayList and the List classes implement the IEnumerable.GetEnumerator() method. For this reason, you can use the foreach loop to access each member of the collection. Here is an example:

using System.Collections;
using static System.Console;

public class Exercise
{
    public static int Main()
    {
        ArrayList molecules = new ArrayList();

        Title = "Chemistry";
        WriteLine("Chemistry");
        WriteLine("---------------------");

        molecules.Add("Ozone");
        molecules.Add("Oxygen");
        molecules.Add("Helium");
        molecules.Add("Sulfuric Acid");
        molecules.Add("Carbon Dioxide");

        foreach (string molecule in molecules)
            WriteLine("Molecule: " + molecule);
        WriteLine("===============================");
        return 0;
    }
}

This would produce:

Chemistry
---------------------
Molecule: Ozone
Molecule: Oxygen
Molecule: Helium
Molecule: Sulfuric Acid
Molecule: Carbon Dioxide
===============================
Press any key to continue . . .

You can use the Item property to change a value in the list. Because the Item property is used to access an existing value from the list, the value must have been created. If you try setting the value of a non-existing item, the compiler would throw an ArgumentOutOfRangeException Exception.

Practical LearningPractical Learning: Iterating Through a Collection

  1. Click the Payrolls.cs tab and change the class as follows:
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace JustInPay1
    {
        public class Payrolls
        {
            . . .
    
            public void DisplayTimeSheets()
            {
                List<TimeSheet> timeSheets = new List<TimeSheet>();
                BinaryFormatter bfTimeSheets = new BinaryFormatter();
                string strTimeSheetsFile = @"C:\Payroll Management\TimeSheets.tss";
    
                Console.WriteLine("Just-In-Pay - Time Sheets");
                Console.Title = "Just-In-Pay - Time Sheets";
    
                using (FileStream fsTimeSheets = new FileStream(strTimeSheetsFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    timeSheets = (List<TimeSheet>)bfTimeSheets.Deserialize(fsTimeSheets);
    
                    Console.WriteLine("=============================================================================");
                    foreach (TimeSheet ts in timeSheets)
                    {
                        Console.WriteLine("Time Sheet #  Employee #  Start Date ");
                        Console.WriteLine("  {0}       {1, -10}  {2}",
                                          ts.TimeSheetNumber, ts.EmployeeNumber, ts.StartDate.ToShortDateString());
                        Console.WriteLine("-----------------------------------------------------------------");
                        Console.WriteLine("       Monday Tuesday Wednesday Thursday Friday Saturday Sunday");
                        Console.WriteLine("-----------------------------------------------------------------");
                        Console.WriteLine("Week 1 {0,5}   {1,5}   {2,5}     {3,5}   {4,5}   {5,5}   {6,5}",
                                          ts.Week1Monday.ToString("F"), ts.Week1Tuesday.ToString("F"),
                                          ts.Week1Wednesday.ToString("F"), ts.Week1Thursday.ToString("F"),
                                          ts.Week1Friday.ToString("F"), ts.Week1Saturday.ToString("F"), ts.Week1Sunday.ToString("F"));
                        Console.WriteLine("Week 2 {0,5}   {1,5}   {2,5}     {3,5}   {4,5}   {5,5}   {6,5}",
                                          ts.Week2Monday.ToString("F"), ts.Week2Tuesday.ToString("F"),
                                          ts.Week2Wednesday.ToString("F"), ts.Week2Thursday.ToString("F"),
                                          ts.Week2Friday.ToString("F"), ts.Week2Saturday.ToString("F"), ts.Week2Sunday.ToString("F"));
                        Console.WriteLine("==================================================================");
                    }
                }
    
                Console.ReadKey();
            }
        }
    }
  2. Click the PayrollPreparation.cs tab and change the class as follows:
    using System.IO;
    using static System.Console;
    
    namespace JustInPay1
    {
        public class PayrollPreparation
        {
            public static int Main(string[] args)
            {
                Directory.CreateDirectory(@"C:\Payroll Management");
    
                WriteLine("Just-in-Pay - Payroll Preparation Services");
                WriteLine("-------------------------------------------------------------------------------");
                WriteLine("Just-In-Pay is a private company that creates and manages payrol on " +
                          "behalf of all types of entities, including individuals, small " +
                          "businesses, enterprises, as well as government agencies. The " +
                          "management involves all aspects of time sheets (or time cards). " +
                          "The Just-In-Pay company spares the hassle of tax laws and " +
                          "related regulations so its clients can concentrate on their most " +
                          "important jobs: taking care of their employees and delivering the " +
                          "best services and products they can to their customers.");
                WriteLine("===============================================================================");
                Write("Press any key to display the main menu...");
    
                ReadKey();
                Clear();
                Payrolls pays = new Payrolls();
                
                Clear();
                pays.DisplayTimeSheets();
                return 0;
            }
        }
    }
  3. To execute the application and see the records, on the main menu, click Debug -> Start Without Debugging
  4. Press any key to display the list of time sheets:
    Just-In-Pay - Time Sheets
    ===========================================================================
    Time Sheet #  Employee #  Start Date
      100001       606384      1/1/2018
    -----------------------------------------------------------------
           Monday Tuesday Wednesday Thursday Friday Saturday Sunday
    -----------------------------------------------------------------
    Week 1  0.00    0.00    0.00      0.00    0.00    8.00    8.00
    Week 2  0.00    0.00    0.00      0.00    0.00    8.00    8.00
    ==================================================================
    Time Sheet #  Employee #  Start Date
      100002       952748      1/1/2018
    -----------------------------------------------------------------
           Monday Tuesday Wednesday Thursday Friday Saturday Sunday
    -----------------------------------------------------------------
    Week 1  8.00    8.00    8.00      8.00    8.00    0.00    0.00
    Week 2  8.00    8.00    8.00      8.00    8.00    0.00    0.00
    ==================================================================
    Time Sheet #  Employee #  Start Date
      100003       941148      1/1/2018
    -----------------------------------------------------------------
           Monday Tuesday Wednesday Thursday Friday Saturday Sunday
    -----------------------------------------------------------------
    Week 1  9.00   10.00    8.50      9.50   10.50   12.00   12.00
    Week 2  8.50    9.00    9.50      8.00   10.00   10.00    8.50
    ==================================================================
    Time Sheet #  Employee #  Start Date
      100004       927048      1/1/2018
    -----------------------------------------------------------------
           Monday Tuesday Wednesday Thursday Friday Saturday Sunday
    -----------------------------------------------------------------
    Week 1  8.00    8.00    8.00      8.00    8.00    0.00    0.00
    Week 2  8.00    8.00    8.00      8.00    8.00    0.00    0.00
    ==================================================================
  5. Press any key to close the window and return to your programming environment

Taking an Action For Each Value or Object

To provide you witha faster means of accessing each item in a collection, the List class is equipped with a method named ForEach. Its syntax is:

public void ForEach(Action<T> action);

This method takes an Action delegate as argument.

Practical LearningPractical Learning: Taking an Action For Each Value or Object

  1. Click the StaffMembers.cs tab and change the class as follows:
    using System.IO;
    using static System.Console;
    using System.Collections.Generic;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace JustInPay1
    {
        public class StaffMembers
        {
            . . .          
    
            private void StaffSummary(Employee empl)
            {
                WriteLine("{0} {1, -10} {2, -10}    {3}     {4}/hr  {5}    {6, 6} {7}",
                                      empl.EmployeeNumber, empl.FirstName, empl.LastName,
                                      empl.State, empl.HourlySalary.ToString("F"),
                                      empl.Exemptions, empl.MaritalStatus, empl.FilingStatus);
                WriteLine("-------------------------------------------------------------------------------");
            }
    
            public void ShowEmployees()
            {
                FileStream fsEmployees = null;
                List<Employee> employees = new List<Employee>();
                BinaryFormatter bfEmployees = new BinaryFormatter();
                string strEmployeesFile = @"C:\Payroll Management\Employees.mpl";
    
                WriteLine("Just-In-Pay - Employees");
                Title = "Just-In-Pay - Employees";
    
                if (File.Exists(strEmployeesFile))
                {
                    using (fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        employees = (List<Employee>)bfEmployees.Deserialize(fsEmployees);
    
                        WriteLine("===============================================================================");
                        WriteLine("                               State    Hourly        Marital     Filing");
                        WriteLine("Empl # First Name Last Name  Residence  Salary exmpts Status      Status");
                        WriteLine("-------------------------------------------------------------------------------");
    
                        employees.ForEach(StaffSummary);
                    }
                }
    
                ReadKey();
            }
        }
    }
  2. Click the PayrollPreparation.cs tab and change the class as follows:
    using System.IO;
    using static System.Console;
    
    namespace JustInPay1
    {
        public class PayrollPreparation
        {
            public static int Main(string[] args)
            {
                Directory.CreateDirectory(@"C:\Payroll Management");
    
                WriteLine("Just-in-Pay - Payroll Preparation Services");
                WriteLine("-------------------------------------------------------------------------------");
                WriteLine("Just-In-Pay is a private company that creates and manages payrol on " +
                          "behalf of all types of entities, including individuals, small " +
                          "businesses, enterprises, as well as government agencies. The " +
                          "management involves all aspects of time sheets (or time cards). " +
                          "The Just-In-Pay company spares the hassle of tax laws and " +
                          "related regulations so its clients can concentrate on their most " +
                          "important jobs: taking care of their employees and delivering the " +
                          "best services and products they can to their customers.");
                WriteLine("===============================================================================");
                Write("Press any key to display the main menu...");
    
                ReadKey();
                Clear();
    
                StaffMembers personnel = new StaffMembers();
    
                personnel.ShowEmployees();
                return 0;
            }
        }
    }
  3. Press any key to show the list of employees:
    Just-In-Pay - Employees
    ===============================================================================
                                   State    Hourly        Marital     Filing
    Empl # First Name Last Name  Residence  Salary exmpts Status      Status
    -------------------------------------------------------------------------------
    941148 Catherine  Watts         MD     26.15/hr  0    Single Head of Household
    -------------------------------------------------------------------------------
    927048 Henry      Meuer         PA     18.95/hr  3    Single Head of Household
    -------------------------------------------------------------------------------
    606384 Herbert    Gibson        MD     22.25/hr  1    Single Head of Household
    -------------------------------------------------------------------------------
    952748 David      Evans         MD     17.25/hr  2    Single Head of Household
    -------------------------------------------------------------------------------
  4. Press any key to close the window and return to your programming environment

Checking Whether a List Contains an Item

Instead of the square brackets that allow you to retrieve a value based on its position, you can look for a value based on its complete definition. You have various options. You can first "build" an item and ask the compiler to check whether any item in the list matches your definition. To perform this search, depending on your class, you can call either the ArrayList.Contains() or the List.Contains() method. The syntax of the System.Collections.ArrayList.Contains() method is:

public virtual bool Contains(object value);

The syntax of the System.Collections.Generic.List.Contains() method is:

public bool Contains(T value);

The value to look for is passed as argument to the method. The compiler would look for the value or object, using the value type or the object definition. If the argument corresponds to an existing value or object, the method returns true. If the value or object is not found, the method returns false. Here is an example:

using System.Collections;
using static System.Console;

public class Exercise
{
    public static int Main()
    {
        ArrayList molecules = new ArrayList();

        Title = "Chemistry";
        WriteLine("Chemistry");
        WriteLine("---------------------------------");

        molecules.Add("Ozone");
        molecules.Add("Oxygen");
        molecules.Add("Helium");
        molecules.Add("Sulfuric Acid");
        molecules.Add("Carbon Dioxide");

        WriteLine("Oxygen is in the list:  " + molecules.Contains("Oxygen"));
        WriteLine("Oxygène is in the list: " + molecules.Contains("Oxygène"));
        WriteLine("=================================");
        return 0;
    }
}

This would produce:

Chemistry
---------------------------------
Oxygen is in the list:  True
Oxygène is in the list: False
=================================
Press any key to continue . . .

Searching for an Item

Another option to look for an item in a list consists of calling a method named BinarySearch supported by both the ArrayList and the List class. It is overloaded in three versions and one of them uses the following syntax:

public virtual int BinarySearch(object value);
public int BinarySearch(T value);

The value to look for is passed as argument to the method. Here is an example:

using System.Collections;
using static System.Console;

public class Exercise
{
    public static int Main()
    {
        ArrayList molecules = new ArrayList();

        Title = "Chemistry";
        WriteLine("Chemistry");
        WriteLine("----------------------------------------------");

        molecules.Add("Hydrogen");
        molecules.Add("Oxygen");

        string strFind = "Oxygen";

        if (molecules.BinarySearch(strFind) > 0)
            WriteLine("Water contains at least one atom of " + strFind + ".");
        else
            WriteLine("Ain't no atom of " + strFind + " in water.");

        WriteLine("----------------------------------------------");

        strFind = "Helium";

        if (molecules.BinarySearch(strFind) > 0)
            WriteLine("Water contains at least one atom of " + strFind + ".");
        else
            WriteLine("Ain't no atom of " + strFind + " in water.");

        WriteLine("==============================================");
        return 0;
    }
}

This would produce:

Chemistry
----------------------------------------------
Water contains at least one atom of Oxygen.
----------------------------------------------
Ain't no atom of Helium in water.
==============================================
Press any key to continue . . .

Checking the Existence of an Item

To let you check whether a certain item exists in a collection, the list classes contains a method named Exists. Its syntax is:

public bool Exists(Predicate<T> match);

This method takes a delegate as argument.

Practical LearningPractical Learning: Checking the Existence of an Item

  1. Access the StaffMembers.cs document and change the class as follows:
    using System.IO;
    using static System.Console;
    using System.Collections.Generic;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace JustInPay1
    {
        public class StaffMembers
        {
            public void Existense()
            {
                List<Employee> employees = new List<Employee>();
                BinaryFormatter bfEmployees = new BinaryFormatter();
                string strFileEmployees = @"C:\Payroll Management\Employees.mpl";
                /* System.Random rndNumber = new System.Random();
                 * string[] emplNumbers = { "941148", "942759", "927048", "606384", "948059", "952748" };
                string strEmployeeNumber = emplNumbers[rndNumber.Next(1, 6)]; */
    
                Write("Type an employee #: ");
                string strEmployeeNumber = ReadLine();
    
                if (File.Exists(strFileEmployees))
                {
                    using (FileStream fsEmployees = new FileStream(strFileEmployees, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        employees = (List<Employee>)bfEmployees.Deserialize(fsEmployees);
    
                        bool exists = employees.Exists((Employee member) =>
                        {
                            return member.EmployeeNumber == strEmployeeNumber;
                        });
    
                        if (exists == true)
                            WriteLine("The database contains an employee with the number " + strEmployeeNumber);
                        else
                            WriteLine("Our database doesn't have an employee with the number " + strEmployeeNumber);
                    }
                }
            }
    
            . . .
        }
    }
  2. Access the PayrollPreparation.cs tab and change the document as follows:
    using System.IO;
    using static System.Console;
    
    namespace JustInPay1
    {
        public class PayrollPreparation
        {
            public static int Main(string[] args)
            {
                Directory.CreateDirectory(@"C:\Payroll Management");
    
                WriteLine("Just-in-Pay - Payroll Preparation Services");
                WriteLine("-------------------------------------------------------------------------------");
                WriteLine("Just-In-Pay is a private company that creates and manages payrol on " +
                          "behalf of all types of entities, including individuals, small " +
                          "businesses, enterprises, as well as government agencies. The " +
                          "management involves all aspects of time sheets (or time cards). " +
                          "The Just-In-Pay company spares the hassle of tax laws and " +
                          "related regulations so its clients can concentrate on their most " +
                          "important jobs: taking care of their employees and delivering the " +
                          "best services and products they can to their customers.");
                WriteLine("===============================================================================");
                Write("Press any key to display the main menu...");
    
                ReadKey();
                Clear();
    
                StaffMembers personnel = new StaffMembers();
    
                personnel.Existense();
                return 0;
            }
        }
    }
  3. To execute the application, on the main menu, click Debug -> Start Without Debugging
  4. Press any key to displa the next window
  5. Type one of the following numbers and press Enter: 941148, 942759, 927048, 606384, 948059, 952748. Here is an example:
    Type an employee #: 927048
    The database contains an employee with the number 927048
    Press any key to continue . . .
    Here is another example:
    Type an employee #: 942759
    Our database doesn't have an employee with the number 942759
    Press any key to continue . . .
  6. Press any key to close the window and return to your programming environment

Finding a Value or Object in a Collection

To let you find an item in a collection, the List class is equipped with a method named Find. Its syntax is:

public T Find(Predicate<T> match);

This method takes a delegate as argument.

Removing a Value or Object from a Collection

Removing an Item by Reference

To remove a value or an object from a collection, you have various options. To first look cfor an item before deleting it, both the ArrayList and the List classes are equipped with a method named Remove. Its syntax is:

public virtual void Remove(object value);
public bool Remove(T value);

This method accepts as argument the value or the object to delete. Here is an example of calling this method:

using System.Collections;
using static System.Console;

public class Exercise
{
    public static void Main()
    {
        ArrayList molecules = new System.Collections.ArrayList();

        molecules.Add("Ozone");
        molecules.Add("Oxygen");
        molecules.Add("Helium");
        molecules.Add("Sulfuric Acid");
        molecules.Add("Carbon Dioxide");

        Title = "Chemistry";

        foreach (string molecule in molecules)
            WriteLine("Molecule: " + molecule);

        molecules.Remove("Helium");

        WriteLine("-----------------------------");

        foreach (string molecule in molecules)
            WriteLine("Molecule: " + molecule);

        WriteLine("=============================");
        return;
    }
}

This would produce:

Molecule: Ozone
Molecule: Oxygen
Molecule: Helium
Molecule: Sulfuric Acid
Molecule: Carbon Dioxide
-----------------------------
Molecule: Ozone
Molecule: Oxygen
Molecule: Sulfuric Acid
Molecule: Carbon Dioxide
=============================
Press any key to continue . . .

Removing an Item by Index

To let you delete an item based on its index within the collection, the classes are equipped with a method named RemoveAt. Its syntax is:

public virtual void RemoveAt(int index);
public void RemoveAt(int index);

With this method, the position of the item is passed as argument. Here is an example:

using System.Collections;
using static System.Console;

public class Exercise
{
    public static void Main()
    {
        ArrayList molecules = new System.Collections.ArrayList();

        molecules.Add("Ozone");
        molecules.Add("Oxygen");
        molecules.Add("Helium");
        molecules.Add("Sulfuric Acid");
        molecules.Add("Carbon Dioxide");

        Title = "Chemistry";

        foreach (string molecule in molecules)
            WriteLine("Molecule: " + molecule);

        molecules.RemoveAt(2);

        WriteLine("-----------------------------");

        foreach (string molecule in molecules)
            WriteLine("Molecule: " + molecule);

        WriteLine("=============================");
        return;
    }
}

This would produce:

Molecule: Ozone
Molecule: Oxygen
Molecule: Helium
Molecule: Sulfuric Acid
Molecule: Carbon Dioxide
-----------------------------
Molecule: Ozone
Molecule: Oxygen
Molecule: Sulfuric Acid
Molecule: Carbon Dioxide
=============================
Press any key to continue . . .

If the position is not valid because either it is lower or higher than the current Count, the compiler would throw an ArgumentOutOfRangeException exception.

Clearing a List

To remove all items from a list at once, you can call the Clear() method of either the ArrayList or the List class. Its syntax is:

Here is an example of calling this method:

using System.Collections;
using static System.Console;

public class Exercise
{
    public static void Main()
    {
        ArrayList molecules = new System.Collections.ArrayList();

        molecules.Add("Ozone");
        molecules.Add("Oxygen");
        molecules.Add("Helium");
        molecules.Add("Sulfuric Acid");
        molecules.Add("Carbon Dioxide");

        Title = "Chemistry";

        foreach (string molecule in molecules)
            WriteLine("Molecule: " + molecule);

        molecules.Clear();

        WriteLine("-----------------------------");

        foreach (string molecule in molecules)
            WriteLine("Molecule: " + molecule);

        WriteLine("=============================");
        return;
    }
}

This would produce:

Molecule: Ozone
Molecule: Oxygen
Molecule: Helium
Molecule: Sulfuric Acid
Molecule: Carbon Dioxide
-----------------------------
=============================
Press any key to continue . . .

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2019, FunctionX Next