Fundamentals of Arrays and Objects

Introduction

You can create an array of objects where each member of the array is based on a formal class or record. Of course, you must have a class or record first. You can use one of the already available classes of the .NET Framework or you can create your own class or record. Here is an example of such a class:

public enum EmploymentStatus
{
    FullTime,
    PartTime,
    Unknown
}

public class Employee
{
    public long EmployeeNumber     { get; set; }
    public string? EmployeeName    { get; set; }
    public EmploymentStatus Status { get; set; }
    public double HourlySalary     { get; set; }
}

Practical LearningPractical Learning: Introducing Arrays and Classes

  1. Start Microsoft Visual Studio and create a Console App (that supports .NET 7.0 (Standard Term Support)) named CountriesStatistics2
  2. On the main menu, click Project -> Add New Item...
  3. In the left list, under Visual C# Items, click Code
  4. In the middle list, click Interface
  5. Change the file name to Abbreviated
  6. Press Enter
  7. Change the document as follows:
    namespace CountriesStatistics2
    {
        internal interface IAbbreviated
        {
            string? Abbreviation { get; set; }
        }
    }
  8. In the Solution Explorer, right-click CountriesStatistics2 -> Add -> Class...
  9. Change the file Name to GovernmentEntity
  10. Click Add
  11. Create a string-based property as follows:
    namespace CountriesStatistics2
    {
        public abstract class GovernmentEntity
        {
            public virtual string? StateName { get; set; }
            public virtual int AreaSqrKms    { get; set; }
            public virtual string? Capital   { get; set; }
        }
    }
  12. In the Solution Explorer, right-click CountriesStatistics2 -> Add -> Class...
  13. Change the file name to State
  14. Click Add
  15. Change the class as follows:
    namespace CountriesStatistics2
    {
        public class State : GovernmentEntity,
                                IAbbreviated
        {
            public string? Abbreviation { get; set; }
        }
    }
  16. In the Solution Explorer, right-click Program.cs and click Rename
  17. Type CountriesStatistics (to get CountriesStatistics.cs) and press Enter
  18. Read the message box and click Yes

Creating an Array of Objects

To create a list of objects, you can declare an array variable and use the square brackets to specify its size. Here is an example:

Employee[] staffMembers = new Employee[3];

public enum EmploymentStatus { FullTime, PartTime, Unknown }

public class Employee
{
    public long EmployeeNumber     { get; set; }
    public string? EmployeeName    { get; set; }
    public EmploymentStatus Status { get; set; }
    public double HourlySalary     { get; set; }
}

You can also use the var or the dynamic keyword to create the array but omit the first square brackets. Here are examples:

var staffMembers = new Employee[8];
dynamic contractors = new Employee[5];

ApplicationPractical Learning: Creating an Array of Objects

Initializing an Array of Objects

If you create an array of objects, you can then access each member using its index, allocate memory for it using the new operator, then access each of its fields or properties, still using its index, to assign it the desired value. Here is an example:

Employee[] staffMembers = new Employee[3];

staffMembers[0] = new Employee();
staffMembers[0].EmployeeNumber = 20204;
staffMembers[0].EmployeeName = "Harry Fields";
staffMembers[0].Status = EmploymentStatus.FullTime;
staffMembers[0].HourlySalary = 16.85;

staffMembers[1] = new Employee();
staffMembers[1].EmployeeNumber = 92857;
staffMembers[1].EmployeeName = "Jennifer Almonds";
staffMembers[1].Status = EmploymentStatus.FullTime;
staffMembers[1].HourlySalary = 22.25;

staffMembers[2] = new Employee();
staffMembers[2].EmployeeNumber = 42963;
staffMembers[2].EmployeeName = "Sharon Culbritt";
staffMembers[2].Status = EmploymentStatus.PartTime;
staffMembers[2].HourlySalary = 10.95;

public enum EmploymentStatus { FullTime, PartTime, Unknown }

public class Employee
{
    public long EmployeeNumber { get; set; }
    public string? EmployeeName { get; set; }
    public EmploymentStatus Status { get; set; }
    public double HourlySalary { get; set; }
}

As an alternative, you can initialize each member of the array when creating it. To do this, you may need a constructor that takes each member you want to initialize, as argument. Here is an example of such a constructor:

public enum EmploymentStatus
{
    FullTime,
    PartTime,
    Unknown
};

public class Employee
{
    public long EmployeeNumber     { get; set; }
    public string? EmployeeName    { get; set; }
    public EmploymentStatus Status { get; set; }
    public double HourlySalary     { get; set; }

    public Employee(long number, string name,
                    EmploymentStatus emplStatus, double salary)
    {
        EmployeeNumber = number;
        EmployeeName = name;
        Status = emplStatus;
        HourlySalary = salary;
    }
}

To initialize an object, before the semi-colon of creating the array, open the curly brackets, allocate memory for each member and specify the value of each field or property. Here are examples:

Employee[] staffMembers = new Employee[]
{
    new Employee(20204, "Harry Fields", EmploymentStatus.FullTime, 16.85),
    new Employee(92857, "Jennifer Almonds", EmploymentStatus.FullTime, 22.25),
    new Employee(42963, "Sharon Culbritt", EmploymentStatus.PartTime, 10.95)
};

If using the var or the dynamic keyword and a constructor to initialize the array, you can omit calling the name of the class or record before the square brackets. Here is an example:

var staffMembers = new[]
{
    new Employee(20204, "Harry Fields", EmploymentStatus.FullTime, 16.85),
    new Employee(92857, "Jennifer Almonds", EmploymentStatus.FullTime, 22.25),
    new Employee(42963, "Sharon Culbritt", EmploymentStatus.PartTime, 10.95)
};

Practical LearningPractical Learning: Initializing an Array of Objects

Accessing the Members of an Array of Objects

Accessing an Object by Index

After creating and initializing the array, you can use it as you see fit. For example, you may want to display its values to the user. You can access any member of the array by its index, then use the same index to get its field(s) and consequently its (their) value(s). Here is an example:

using static System.Console;

Employee[] staffMembers = new Employee[]
{
    new Employee(20204, "Harry Fields", EmploymentStatus.FullTime, 16.85),
    new Employee(92857, "Jennifer Almonds", EmploymentStatus.FullTime, 22.25),
    new Employee(42963, "Sharon Culbritt", EmploymentStatus.PartTime, 10.95)
};

Title = "Employee Record";

WriteLine("Employee Record");
WriteLine("------------------------------------");
WriteLine("Employee #:    {0}", staffMembers[2].EmployeeNumber);
WriteLine("Full Name:     {0}", staffMembers[2].EmployeeName);
WriteLine(string.Format("Status:        {0}", staffMembers[2].Status));
WriteLine($"Hourly Salary: {staffMembers[2].HourlySalary}");

WriteLine("====================================");

public enum EmploymentStatus { FullTime, PartTime, Unknown }

internal class Employee
{
    public long EmployeeNumber     { get; set; }
    public string? EmployeeName    { get; set; }
    public EmploymentStatus Status { get; set; }
    public double HourlySalary     { get; set; }

    public Employee(long number, string name,
                    EmploymentStatus emplStatus, double salary)
    {
        EmployeeNumber = number;
        EmployeeName = name;
        Status = emplStatus;
        HourlySalary = salary;
    }
}

This would produce:

Employee Record
------------------------------------
Employee #:    42963
Full Name:     Sharon Culbritt
Status:        PartTime
Hourly Salary: 10.95
====================================
Press any key to close this window . . .

Once again, remember that the index you use must be between 0 (included) and the number of objects - 1. Otherwise, the application would throw an exception.

Looping to Access an Object

You can use a loop (while, do...while, or for) to access a member of the array using the member's index.

Practical LearningPractical Learning: Looping for an Array of Objects

  1. To present the list of states, add the follow code:
    using static System.Console;
    
    State[] states = new State[6];
    
    states[0] = new State() { Abbreviation = "WA ", StateName = "Western Australia", AreaSqrKms = 2529875, Capital = "Perth" };
    states[1] = new State() { Abbreviation = "SA ", StateName = "South Australia  ", AreaSqrKms = 983482, Capital = "Adelaide" };
    states[2] = new State() { Abbreviation = "QLD", StateName = "Queensland       ", AreaSqrKms = 1730648, Capital = "Brisbane" };
    states[3] = new State() { Abbreviation = "NSW", StateName = "New South Wales  ", AreaSqrKms = 800642, Capital = "Sydney" };
    states[4] = new State() { Abbreviation = "VIC", StateName = "Victoria         ", AreaSqrKms = 227416, Capital = "Melbourne" };
    states[5] = new State() { Abbreviation = "TAS", StateName = "Tasmania         ", AreaSqrKms = 68401, Capital = "Hobart" };
    
    WriteLine("Countries Statistics");
    WriteLine("==========================================================");
    WriteLine(" # Abbrv State Name           Area (kms)    Capital");
    WriteLine("==========================================================");
    
    for (int counter = 0; counter < states.Length - 1; counter++)
    {
        WriteLine($" {counter + 1}  {states[counter].Abbreviation}  {states[counter].StateName}    {states[counter].AreaSqrKms,10}    {states[counter].Capital}");
        WriteLine("----------------------------------------------------------");
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging:
    Countries Statistics
    ==========================================================
     # Abbrv State Name           Area (kms)    Capital
    ==========================================================
     1  WA   Western Australia       2529875    Perth
    ----------------------------------------------------------
     2  SA   South Australia          983482    Adelaide
    ----------------------------------------------------------
     3  QLD  Queensland              1730648    Brisbane
    ----------------------------------------------------------
     4  NSW  New South Wales          800642    Sydney
    ----------------------------------------------------------
     5  VIC  Victoria                 227416    Melbourne
    ----------------------------------------------------------
    
    Press any key to close this window . . .
  3. Close the window and return to your programming environment

A Field as an Array

Introduction

Like a primitive type, an array of objects can be made a field of a class or record. You can primarily declare the array and specify its size. Here is an example:

public class CompanyRecords
{
    Employee[] employees = new Employee[12];
}

After doing this, you can initialize the array from the index of each member. Alternatively, you can declare the array in the body of the class or record, then use a constructor or another method of the class or record to allocate memory for it. Here is an example:

public class Employee
{

}

public class CompanyRecords
{
    Employee[] employees;

    public CompanyRecords()
    {
        employees = new Employee[12];
    }
}

Initializing a Field of Array Type

To initialize the array, remember that each member is an object whose memory must be allocated on the heap. Therefore, apply the new operator on each member of the array to allocate memory for it, and then initialize it. Here is an example:

public enum EmploymentStatus
{
    FullTime,
    PartTime,
    Unknown
};

internal class Employee
{
    public long EmployeeNumber { get; set; }
    public string? EmployeeName { get; set; }
    public EmploymentStatus Status { get; set; }
    public double HourlySalary { get; set; }
}

internal class CompanyRecords
{
    Employee[] employees;

    public CompanyRecords()
    {
        employees = new Employee[2];

        employees[0] = new Employee();
        employees[0].EmployeeNumber = 70128;
        employees[0].EmployeeName = "Frank Dennison";
        employees[0].Status = EmploymentStatus.PartTime;
        employees[0].HourlySalary = 8.65;

        employees[1] = new Employee();
        employees[1].EmployeeNumber = 24835;
        employees[1].EmployeeName = "Jeffrey Arndt";
        employees[1].Status = EmploymentStatus.Unknown;
        employees[1].HourlySalary = 16.05;
    }
}

If the class or record used as field has an appropriate constructor, you can use it to initialize each member of the array. Here is an example:

internal class CompanyRecords
{
    Employee[] employees;

    public CompanyRecords()
    {
    	employees = new Employee[]
        {
            new Employee(70128, "Justine Hearson", EmploymentStatus.PartTime, 10.62),
            new Employee(24835, "Bertha Hack", EmploymentStatus.FullTime, 18.94),
            new Employee(70128, "Frank Dennison", EmploymentStatus.Seasonal, 12.48),
            new Employee(24835, "Jeffrey Arndt", EmploymentStatus.PartTime, 16.05),
        };
    }
}

Using the Array

Once you have created and initialized the array, you can use it as you see fit, such as displaying its values to the user. You must be able to access each member of the array, using its index. Once you have accessed a member of the array, you can get to its fields.

Properties of Array Types

The Value of a Property from an Array

You can create a property in a class or record so that the property is an array. If you want to use a property that includes both a getter and a setter sections, you can use a field that was created as an array. Here is an example:

internal class Region
{
    public string? Designation { get; set; }
    public string? Description { get; set; }
}

internal class State
{
    private Region[] rgn;

    public Region[] Area
    {
        get
        {
            return rgn;
        }
        set
        {
            rgn = value;
        }
    }
}

Otherwise, you can create the property as an automatic one.

To specify the value of a property of array type, you can get an array member using its index and assign it to the desired member. Here are examples:

using static System.Console;

Region[] regions = new Region[]
{
    new Region() { Designation = "East North Central" },
    new Region() { Designation = "East South Central" },
    new Region() { Designation = "New England"        }
};

State[] states = new State[]
{
    new State() { Region = regions[1], Abbreviation = "KY", StateName = "Kentucky" },
    new State() { StateName = "Maine", Region = regions[2], AreaSqrMiles =  35387 },
    new State() { Capital = "Boston", StateName = "Massachusetts", Region = regions[2] },
    new State() { Abbreviation = "AL", Region = regions[1], StateName = "Alabama" },
    new State() { Region = regions[0], StateName = "Michigan", AreaSqrMiles = 98810 }
};

WriteLine("====================================");

internal class Region
{
    public string? Designation { get; set; }
    public string? Description { get; set; }
}

internal class State
{
    internal Region Region { get; set; }
    private Region[] rgn;

    public Region[] Area
    {
        get
        {
            return rgn;
        }
        set
        {
            rgn = value;
        }
    }

    public string? Abbreviation { get; internal set; }
    public string? StateName { get; internal set; }
    public int AreaSqrMiles { get; internal set; }
    public string? Capital { get; set; }
}

A Property of an Array Type

A property can be an array type. In this case, the values of the property would be held by an array.

Practical LearningPractical Learning: Creating a Property of Array Type

Initializing a Property of Array Type

To initialize a property that is an array or to specify its value(s), you can create an array and assign it to the property. You can also create the array directly on the property. If the main class is used as an array, all properties can have the same number of values or each property can have a different number of values.

Practical LearningPractical Learning: Initializing the Properties of Array Type

Accessing the Property

To get the value of each element of a property that is an array type, you can apply the square brackets to the property and pass the index of the desired element.

Practical LearningPractical Learning: Accessing a Property of an Array Type

  1. To use the array property, change the code as follows:
    using static System.Console;
    using CountriesStatistics2;
    
    State[] states = new State[6];
    
    string[] WesternAustralia = new string[3]
    {
        "Perth", "Ellenbrook", "Geraldton"
    };
    
    states[0] = new State() { Abbreviation = "WA ", StateName = "Western Australia", AreaSqrKms = 2529875, Capital = "Perth    " };
    states[0].SignificantCities = WesternAustralia;
    
    states[1] = new State() { Abbreviation = "SA ", StateName = "South Australia  ", AreaSqrKms =  983482, Capital = "Adelaide " };
    states[1].SignificantCities = new string[]
    {
        "Adelaide", "Mount Gambier", "Port Lincoln"
    };
    
    states[2] = new State() 
    {
        Abbreviation = "QLD",
        StateName = "Queensland       ",
        AreaSqrKms = 1730648,
        Capital = "Brisbane ",
        SignificantCities = new string[] { "Brisbane", "Sunshine Coast", "Gold Coast" }
    };
    
    states[3] = new State()
    {
        Abbreviation = "NSW", StateName = "New South Wales  ", AreaSqrKms = 800642, Capital = "Sydney   ",
        SignificantCities = new string[] { "Sydney", "Newcastle", "Queanbeyan" }
    };
    states[4] = new State() { Abbreviation = "VIC", StateName = "Victoria         ", AreaSqrKms = 227416, Capital = "Melbourne" };
    states[5] = new State() { Abbreviation = "TAS", StateName = "Tasmania         ", AreaSqrKms =  68401, Capital = "Hobart    " };
    
    states[4].SignificantCities = new string[] { "Geelong", "Ballarat", "Bendigo" };
    
    // These are the significant cities for Tasmania
    states[5].SignificantCities = new string[4];
    states[5].SignificantCities[0] = "Devonport";
    states[5].SignificantCities[1] = "Hobart";
    states[5].SignificantCities[2] = "Strahan";
    
    WriteLine("Countries Statistics");
    WriteLine("==============================================================================================");
    WriteLine(" # Abbrv State Name           Area (kms)    Capital     Significant Cities");
    WriteLine("==============================================================================================");
    
    for (int counter = 0; counter < states.Length - 1; counter++)
    {
        WriteLine($" {counter + 1}  {states[counter].Abbreviation}  {states[counter].StateName}    {states[counter].AreaSqrKms,10}    {states[counter].Capital}   " + 
        states[counter].SignificantCities[0] + ", " + states[counter].SignificantCities[1] + ", " + states[counter].SignificantCities[2]);
    
        WriteLine("----------------------------------------------------------------------------------------------");
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging:
    Countries Statistics
    ==============================================================================================
     # Abbrv State Name           Area (kms)    Capital     Significant Cities
    ==============================================================================================
     1  WA   Western Australia       2529875    Perth       Perth, Ellenbrook, Geraldton
    ----------------------------------------------------------------------------------------------
     2  SA   South Australia          983482    Adelaide    Adelaide, Mount Gambier, Port Lincoln
    ----------------------------------------------------------------------------------------------
     3  QLD  Queensland              1730648    Brisbane    Brisbane, Sunshine Coast, Gold Coast
    ----------------------------------------------------------------------------------------------
     4  NSW  New South Wales          800642    Sydney      Sydney, Newcastle, Queanbeyan
    ----------------------------------------------------------------------------------------------
     5  VIC  Victoria                 227416    Melbourne   Geelong, Ballarat, Bendigo
    ----------------------------------------------------------------------------------------------
    
    Press any key to close this window . . .
  3. Close the window and return to your programming environment

Arrays of Objects and Methods

Passing an Array of Objects as Argument

As done for an array of a primitive type, you can pass an array of objects as arguments. You follow the same rules we reviewed; that is, in the parentheses of a method, enter the class or record name, the empty square brackets, and the name of the argument. Here is an example:

public class CompanyRecords
{
    public CompanyRecords(Employee[] employees)
    {
    }
}

You can then access each member of the argument and do what you judge necessary. For example, you can display the values that the argument is holding. To call a method that takes an array of objects, in its parentheses, just enter the name of the array. Here is an example:

public class CompanyRecords
{
    public CompanyRecords(Employee[] employees)
    {
         Employees = new Employee[]
         {
             new Employee(70128, "Justine Hearson", EmploymentStatus.PartTime, 10.62),
             new Employee(24835, "Bertha Hack", EmploymentStatus.FullTime, 18.94),
             new Employee(70128, "Frank Dennison", EmploymentStatus.Seasonal, 12.48),
             new Employee(24835, "Jeffrey Arndt", EmploymentStatus.PartTime, 16.05),
         };
    }
}

public class Exercise
{
    private void Present()
    {
        Employee[] Values = new Employee[4];

        CompanyRecords records = new CompanyRecords(Values);
    }
}

As stated for arrays of primitive types, an array of objects passed as argument is treated as a reference. You can use this characteristic of arrays to initialize the array. You can also indicate that the array is passed by reference by preceding its name with the ref keyword.

Returning an Array of Objects

An array of objects can be returned from a method. To indicate this when defining the method, first type the name of the class or record followed by square brackets. Here is an example:

public class CompanyRecords
{
    public Employee[] RegisterEmployees()
    {
    }
}

In the body of the method, you can take care of anythin you want. The major rule to follow is that, before exiting the method, you must return an array of the class or record indicated on the left side of the method name.

To use the method, you can simply call it. If you want, since the method returns an array, you can retrieve that list and store it in a local array for later use. Here is an example:

using System;
using static System.Environment;
using static System.Windows.Forms.MessageBox;

public enum EmploymentStatus
{
    FullTime,
    PartTime,
    Seasonal,
    Unknown
}

public class Employee
{
    public long EmployeeNumber { get; set; }
    public string? EmployeeName { get; set; }
    public EmploymentStatus Status { get; set; }
    public double HourlySalary { get; set; }

    public Employee(long number, string name,
                    EmploymentStatus emplStatus, double salary)
    {
        EmployeeNumber = number;
        EmployeeName = name;
        Status = emplStatus;
        HourlySalary = salary;
    }
}

public class CompanyRecords
{
    public Employee[] RegisterEmployees()
    {
        Employee[] Employees = new Employee[]
        {
             new Employee(70128, "Justine Hearson", EmploymentStatus.PartTime, 10.62),
             new Employee(24835, "Bertha Hack", EmploymentStatus.FullTime, 18.94),
             new Employee(70128, "Frank Dennison", EmploymentStatus.Seasonal, 12.48),
             new Employee(24835, "Jeffrey Arndt", EmploymentStatus.PartTime, 16.05),
        };

        return Employees;
    }
}

public class Exercise
{
    public void Present()
    {
        CompanyRecords records = new CompanyRecords();

        Employee[] contractors = records.RegisterEmployees();
        records.PrepareRecords(ref contractors);
    }
}

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2023, FunctionX Thursday 14 October 2021 Next