An Array in a Class

A Field as an Array

In a class, you can declare a variable that is an array. This means that, in a class, you can create a field. In the same way, you can declare as many array variables you want. Here are examples:

public class StaffPayroll
{
    double[] times;
}

You can then use the array. Of course, you must initialize the array before using it. You have many options. You can initialize the array where it's variable is declared. Here is example:

public class StaffPayroll
{
    double[] times = new double[5];
}

You can declare the array variable in the body of a class and then initialize it a constructor in another method of the class. Here is example:

public class StaffPayroll
{
    double[] times;

    public void Present()
    {
        times = new double[5];
    }
}

You can declare an array variable in one class and initialize it outside the class such as in a method of another class. Either way, the rule is that the array must hold some value(s) before it can be used. This means that, after creating an array and initializing it, you can access each one of its members and values and assign the desired value to it, then use the array. Here is an example:

using static System.Console;

StaffPayroll sp = new();

public class StaffPayroll
{
    // Declaring an array without initializing it
    string[] fullName;

    public StaffPayroll()
    {
        // Initializing the array variable
        fullName = new string[3];

        fullName[0] = "Frank";
        fullName[1] = "Michael";
        fullName[2] = "Lombardi";

        Prepare();

        WriteLine("Payroll Summary");
        WriteLine("--------------------------------------------");
        WriteLine("Employee Name:       {0} {1} {2}",
            fullName[0], fullName[1], fullName[2]);

        Present();
    }

    // Declaring and initializing an array
    double[] times = new double[5];

    private void Prepare()
    {
        // Setting the values of the array that was declared and initialized
        times[0] = 6.00;
        times[1] = 8.50;
        times[2] = 7.00;
        times[3] = 5.50;
        times[4] = 6.50;
    }

    public void Present()
    {
        WriteLine("============================================");
        WriteLine("Time Worked");
        WriteLine("--------------------------------------------");
        // Accessing the array that was declared and initialized
        WriteLine("     Monday:         {0:N}", times[0]);
        WriteLine("     Tuesday:        {0:N}", times[1]);
        WriteLine("     Wednesday:      {0:N}", times[2]);
        WriteLine("     Thursday:       {0:N}", times[3]);
        WriteLine("     Friday:         {0:N}", times[4]);
        WriteLine("--------------------------------------------");
        WriteLine("Total Time Worked:   {0:N}",
                  times[0] + times[1] +
                  times[2] + times[3] + times[4]);
        WriteLine("============================================");
    }
}

This would produce:

Payroll Summary
--------------------------------------------
Employee Name:       Frank Michael Lombardi
============================================
Time Worked
--------------------------------------------
     Monday:         6.00
     Tuesday:        8.50
     Wednesday:      7.00
     Thursday:       5.50
     Friday:         6.50
--------------------------------------------
Total Time Worked:   33.50
============================================

Press any key to close this window . . .

The above two techniques are used if you don't have the values of the array when declaring the variable. If you have those values, you can provide them immediately exactly as we have been introduced to array variables so far. Here are two examples:

using static System.Console;

StaffPayroll sp = new();

public record StaffPayroll
{
    // An array variable of strings
    string[] fullName = new string[3] { "Frank", "Michael", "Lombardi"};
    // An array variable of numbers
    double[] times = new double[5] {  6.00, 8.50, 7.00, 5.50, 6.50 };

    public StaffPayroll()
    {
        WriteLine("Payroll Summary");
        WriteLine("--------------------------------------------");
        WriteLine("Employee Name:       {0} {1} {2}",
            fullName[0], fullName[1], fullName[2]);
        WriteLine("============================================");
        WriteLine("Time Worked");
        WriteLine("--------------------------------------------");
        // Accessing the array that was declared and initialized
        WriteLine("     Monday:         {0:N}", times[0]);
        WriteLine("     Tuesday:        {0:N}", times[1]);
        WriteLine("     Wednesday:      {0:N}", times[2]);
        WriteLine("     Thursday:       {0:N}", times[3]);
        WriteLine("     Friday:         {0:N}", times[4]);
        WriteLine("--------------------------------------------");
        WriteLine("Total Time Worked:   {0:N}",
                  times[0] + times[1] +
                  times[2] + times[3] + times[4]);
        WriteLine("============================================");
    }
}

A Complete Property as an Array

In a class, you can create a property whose type is an array. To do this, when creating the property, apply the square brackets to its type. If you are creating a complete property, first create its field as an array. When creating its property, apply the square brackets to its type to indicate that it is an array. Such a property can be created as follows:

Payroll pay = new();

pay.EmployeeNumber = 280_735;

internal class Payroll
{
    int nbr;
    string[] name;

    public int EmployeeNumber
    {
        get { return nbr;  }
        set { nbr = value; }
    }
    
    public string[] EmployeeName
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }
}

Before accessing the values of the property, you must initialize it. You have many options. As one solution, you can create a method or a constructor in the class. In the method or the constructor, initialize the property using the new operator and specify the dimension of the array. In the method or outside the class, you can then access each array element and either assign a value to it or both assign a value to it then access that value. Here is an example that initializes an array property in a constructor:

using static System.Console;

Payroll pay = new();

pay.EmployeeNumber = 280_735;

WriteLine("Payroll Summary");
WriteLine("-----------------------------------");
WriteLine("Employee #:    {0}", pay.EmployeeNumber);
WriteLine("Employee Name: {0} {1} {2}",
          pay.EmployeeName[0], pay.EmployeeName[1], pay.EmployeeName[2]);
WriteLine("===================================");

internal class Payroll
{
    int nbr;
    string[] name;

    public Payroll()
    {
        name = new string[3];
        name[0] = "Ronald";
        name[1] = "Denis";
        name[2] = "Paulson";
    }

    public int EmployeeNumber
    {
        get { return nbr;  }
        set { nbr = value; }
    }

    public string[] EmployeeName
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }
}

This would produce:

Payroll Summary
-----------------------------------
Employee #:    280735
Employee Name: Ronald Denis Paulson
===================================

Press any key to close this window . . .

As another solution, you can access the property outside the class and initialize it. Here is an example:

using static System.Console;

StaffPayroll pay = new();

pay.EmployeeNumber = 280_735;

pay.EmployeeName = new string[2];
pay.EmployeeName[0] = "Michael";
pay.EmployeeName[1] = "Simms";

WriteLine("Payroll Summary");
WriteLine("-----------------------------------");
WriteLine("Employee #:          {0}", pay.EmployeeNumber);
WriteLine("Employee Name:       {0} {1}", pay.EmployeeName[0], pay.EmployeeName[1]);
WriteLine("===================================");

internal record StaffPayroll
{
    int nbr;
    string[] name;

    public int EmployeeNumber
    {
        get { return nbr;  }
        set { nbr = value; }
    }

    public string[] EmployeeName
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }
}

This would produce:

Payroll Summary
-----------------------------------
Employee #:          280735
Employee Name:       Michael Simms
===================================

Press any key to close this window . . .

An Automatic Property as an Array

You already know that if you want a simple property, you can create it as an automatic one. In this case, simply add the square brackets to the property's data type. Here are two examples:

StaffPayroll pay = new();

pay.EmployeeNumber = 280_735;

internal record StaffPayroll
{
    public int EmployeeNumber { get; set; }
    public string[] EmployeeName { get; set; }
    public double[] WeeklyTime { get; set; }
}

Once again, before accessing the values of the property, you must initialize it and there are many ways you can do that. As one option, you can access the property and initialize it with the new operator and specify the dimension of the array. After that, you can acccess each array element and either assign a value to it or both assign a value to it then access that value. Here are examples:

using static System.Console;

StaffPayroll pay = new();

pay.EmployeeNumber = 280_735;

// Initializing the property
pay.EmployeeName = new string[2];
// Accessing each array element
pay.EmployeeName[0] = "Robert";
pay.EmployeeName[1] = "Simms";

// Initializing the property
pay.WeeklyTime = new double[5];
pay.WeeklyTime[0] = 8.00;
pay.WeeklyTime[1] = 6.50;
pay.WeeklyTime[2] = 9.00;
pay.WeeklyTime[3] = 8.50;
pay.WeeklyTime[4] = 7.50;

WriteLine("Payroll Summary");
WriteLine("-----------------------------------");
WriteLine("Employee #:          {0}", pay.EmployeeNumber);
WriteLine("Employee Name:       {0} {1}", pay.EmployeeName[0], pay.EmployeeName[1]);
WriteLine("===================================");
WriteLine("Time Worked");
WriteLine("-----------------------------------");
WriteLine("     Monday:         {0:N}", pay.WeeklyTime[0]);
WriteLine("     Tuesday:        {0:N}", pay.WeeklyTime[1]);
WriteLine("     Wednesday:      {0:N}", pay.WeeklyTime[2]);
WriteLine("     Thursday:       {0:N}", pay.WeeklyTime[3]);
WriteLine("     Friday:         {0:N}", pay.WeeklyTime[4]);
WriteLine("-----------------------------------");
WriteLine("Total Time Worked:   {0:N}",
          pay.WeeklyTime[0] + pay.WeeklyTime[1] + 
          pay.WeeklyTime[2] + pay.WeeklyTime[3] + pay.WeeklyTime[4]);
WriteLine("====================================");

internal record StaffPayroll
{
    public int EmployeeNumber { get; set; }
    public string[] EmployeeName { get; set; }
    public double[] WeeklyTime { get; set; }
}

This would produce:

Payroll Summary
-----------------------------------
Employee #:          280735
Employee Name:       Robert Simms
===================================
Time Worked
-----------------------------------
     Monday:         8.00
     Tuesday:        6.50
     Wednesday:      9.00
     Thursday:       8.50
     Friday:         7.50
-----------------------------------
Total Time Worked:   39.50
====================================

Press any key to close this window . . .

You can also initialize the automatic array property in a method or a constructor of the class. One more option is that you can initialize the automatic array property after it's closing curly bracket. This can be done as follows:

using static System.Console;

StaffPayroll pay = new();

pay.EmployeeNumber = 974_597;

WriteLine("Payroll Summary");
WriteLine("-----------------------------------");
WriteLine("Employee #:          {0}", pay.EmployeeNumber);
WriteLine("Employee Name:       {0} {1} {2}",
          pay.EmployeeName[0], pay.EmployeeName[1], pay.EmployeeName[2]);
WriteLine("===================================");
WriteLine("Time Worked");
WriteLine("-----------------------------------");
WriteLine("     Monday:         {0:N}", pay.WeeklyTime[0]);
WriteLine("     Tuesday:        {0:N}", pay.WeeklyTime[1]);
WriteLine("     Wednesday:      {0:N}", pay.WeeklyTime[2]);
WriteLine("     Thursday:       {0:N}", pay.WeeklyTime[3]);
WriteLine("     Friday:         {0:N}", pay.WeeklyTime[4]);
WriteLine("-----------------------------------");
WriteLine("Total Time Worked:   {0:N}",
          pay.WeeklyTime[0] + pay.WeeklyTime[1] + 
          pay.WeeklyTime[2] + pay.WeeklyTime[3] + pay.WeeklyTime[4]);
WriteLine("====================================");

internal class StaffPayroll
{
    int nbr;
    string[] name;

    public Payroll()
    {
        name = new string[3];

        name[0] = "Jeannine";
        name[1] = "Denise";
        name[2] = "Richards";
    }

    public int EmployeeNumber
    {
        get { return nbr;  }
        set { nbr = value; }
    }

    public string[] EmployeeName
    {
        get { return name;  }
        set { name = value; }
    }

    public double[] WeeklyTime { get; set; } = new double[5] { 8.00, 6.50, 9.00, 8.50, 7.50 };
}

Arrays and Methods

Producing an Array

We have already studied how to involve array with functions. The same techniques are applied to methods of a class. The only difference is that you will need and object of the class to access. To create a method that produces an array, specify an array as its return type. In the body of the method, you can create an array variable, initialize that array, and return that variable. You can then call that method when you need its returned array. Here is an example:

using static System.Console;

StaffPayroll pay = new();

pay.EmployeeNumber = 974_597;

WriteLine("Payroll Summary");
WriteLine("--------------------------------------");
WriteLine("Employee #:    {0}", pay.EmployeeNumber);
WriteLine("Employee Name: {0} {1} {2}",
          pay.EmployeeName[0], pay.EmployeeName[1], pay.EmployeeName[2]);
WriteLine("======================================");

internal class StaffPayroll
{
    int nbr;

    public int EmployeeNumber
    {
        get { return nbr;  }
        set { nbr = value; }
    }

    private string[] Create()
    {
        string[] names = new string[3];

        names[0] = "William";
        names[1] = "Jeremy";
        names[2] = "Gonzalez";

        return names;
    }

    public string[] EmployeeName
    {
        get
        {
            return Create();
        }
    }
}

This would produce:

Payroll Summary
--------------------------------------
Employee #:    974597
Employee Name: William Jeremy Gonzalez
======================================

Press any key to close this window . . .

Passing an Array

To create a parameterized array, in the parentheses of a method, specify the type of a parameter as an array. When calling the method, you must pass an array as argument. You can first declare and initialize an array, then pass the variable to the method. Here is an example:

using static System.Console;

var ts = new double[] { 10.00, 8.50, 9.00, 8.00, 9.50 };

StaffPayroll pay = new(208_508, ts);

internal record StaffPayroll
{
    int nbr;

    public StaffPayroll(int number, double[] numbers)
    {
        string[] nm = Create();

        WriteLine("Payroll Summary");
        WriteLine("------------------------------------");
        WriteLine("Employee #:    {0}", number);
        WriteLine("Employee Name: {0} {1}", nm[0], nm[1]);
        WriteLine("====================================");
        WriteLine("Time Worked");
        WriteLine("------------------------------------");
        WriteLine("     Monday:         {0:N}", numbers[0]);
        WriteLine("     Tuesday:        {0:N}", numbers[1]);
        WriteLine("     Wednesday:      {0:N}", numbers[2]);
        WriteLine("     Thursday:       {0:N}", numbers[3]);
        WriteLine("     Friday:         {0:N}", numbers[4]);
        WriteLine("------------------------------------");
        WriteLine("Total Time Worked:   {0:N}",
                  numbers[0] + numbers[1] +
                  numbers[2] + numbers[3] + numbers[4]);
        WriteLine("====================================");
    }

    public int EmployeeNumber
    {
        get { return nbr; }
        set { nbr = value; }
    }

    private string[] Create()
    {
        string[] names = new string[2] { "Catherine", "Campbell" };

        return names;
    }

    public string[] EmployeeName
    {
        get { return Create(); }
    }
}

This would produce:

Payroll Summary
------------------------------------
Employee #:    208508
Employee Name: Catherine Campbell
====================================
Time Worked
------------------------------------
     Monday:         10.00
     Tuesday:        8.50
     Wednesday:      9.00
     Thursday:       8.00
     Friday:         9.50
------------------------------------
Total Time Worked:   45.00
====================================

Press any key to close this window . . .

By the way, when you are calling the method, if you already know the values of the array, you can pass that array directly to the method you are calling. Here is an example:

using static System.Console;

StaffPayroll pay = new(208_508, new double[] { 10.00, 8.50, 9.00, 8.00, 9.50 });

internal class StaffPayroll
{
    int nbr;

    public StaffPayroll(int number, double[] numbers)
    {
        string[] nm = Create();

        WriteLine("Payroll Summary");
        WriteLine("------------------------------------");
        WriteLine("Employee #:    {0}", number);
        WriteLine("Employee Name: {0} {1}", nm[0], nm[1]);
        WriteLine("====================================");
        WriteLine("Time Worked");
        WriteLine("------------------------------------");
        WriteLine("     Monday:         {0:N}", numbers[0]);
        WriteLine("     Tuesday:        {0:N}", numbers[1]);
        WriteLine("     Wednesday:      {0:N}", numbers[2]);
        WriteLine("     Thursday:       {0:N}", numbers[3]);
        WriteLine("     Friday:         {0:N}", numbers[4]);
        WriteLine("------------------------------------");
        WriteLine("Total Time Worked:   {0:N}",
                  numbers[0] + numbers[1] +
                  numbers[2] + numbers[3] + numbers[4]);
        WriteLine("====================================");
    }

    public int EmployeeNumber
    {
        get { return nbr; }
        set { nbr = value; }
    }

    private string[] Create()
    {
        return new string[2] { "Catherine", "Campbell" };
    }

    public string[] EmployeeName
    {
        get { return Create(); }
    }
}

Topics on Arrays and Classes

A Read-Only Array

Usually when you declare an array variable in a class (an array as opposed to other types of collections), you may already know the values you want the array to hold, and most of the time, you as the programmer will provide those values as opposed to the user providing them. To assist the compiler, you can create such an array as a read-only object. To do this, apply the readonly keyword to the variable. This can be done as follows:

using static System.Console;

StaffPayroll sp = new();

public class StaffPayroll
{
    // Declaring an array without initializing it
    readonly string[] fullName;

    // Declaring and initializing an array
    readonly double[] week1TimeWorked = new double[5];
    // An array variable of numbers
    readonly double[] week2TimeWorked = new double[5] { 6.00, 8.50, 7.00, 5.50, 6.50 };

    public StaffPayroll()
    {
        // Initializing the array variable
        fullName = new string[3];

        fullName[0] = "Bethanie";
        fullName[1] = "Elizabeth";
        fullName[2] = "Gants";

        WriteLine("Payroll Summary");
        WriteLine("-----------------------------------------------");
        WriteLine("Employee Name:       {0} {1} {2}",
            fullName[0], fullName[1], fullName[2]);

        Prepare();
        Present();
    }

    private void Prepare()
    {
        // Setting the values of the array that was declared and initialized
        week1TimeWorked[0] = 8.00;
        week1TimeWorked[1] = 6.00;
        week1TimeWorked[2] = 9.00;
        week1TimeWorked[3] = 8.50;
        week1TimeWorked[4] = 7.50;
    }

    public void Present()
    {
        WriteLine("===============================================");
        WriteLine("Time Worked");
        WriteLine("-----------------------------------------------");
        WriteLine("Week 1");
        WriteLine("     Monday:         {0:N}", week1TimeWorked[0]);
        WriteLine("     Tuesday:        {0:N}", week1TimeWorked[1]);
        WriteLine("     Wednesday:      {0:N}", week1TimeWorked[2]);
        WriteLine("     Thursday:       {0:N}", week1TimeWorked[3]);
        WriteLine("     Friday:         {0:N}", week1TimeWorked[4]);
        WriteLine("-----------------------------------------------");
        WriteLine("Week 2");
        WriteLine("     Monday:         {0:N}", week2TimeWorked[0]);
        WriteLine("     Tuesday:        {0:N}", week2TimeWorked[1]);
        WriteLine("     Wednesday:      {0:N}", week2TimeWorked[2]);
        WriteLine("     Thursday:       {0:N}", week2TimeWorked[3]);
        WriteLine("     Friday:         {0:N}", week2TimeWorked[4]);
        WriteLine("-----------------------------------------------");
        WriteLine("Total Time Worked:   {0:N}",
                  week1TimeWorked[0] + week1TimeWorked[1] + week1TimeWorked[2] +
                  week1TimeWorked[3] + week1TimeWorked[4] + week2TimeWorked[0] + 
                  week2TimeWorked[1] + week2TimeWorked[2] + week2TimeWorked[3] + 
                  week2TimeWorked[4]);
        WriteLine("===============================================");
    }
}

This would produce:

Payroll Summary
-----------------------------------------------
Employee Name:       Bethanie Elizabeth Gants
===============================================
Time Worked
-----------------------------------------------
Week 1
     Monday:         8.00
     Tuesday:        6.00
     Wednesday:      9.00
     Thursday:       8.50
     Friday:         7.50
-----------------------------------------------
Week 2
     Monday:         6.00
     Tuesday:        8.50
     Wednesday:      7.00
     Thursday:       5.50
     Friday:         6.50
-----------------------------------------------
Total Time Worked:   72.50
===============================================

Press any key to close this window . . .

The Nullity of a Property Array

If you create a property whose type is an array, if, or as long as, the property is not initialized, the compiler would issue a warning, which you should deal with. You have many options to address the issue. One solution is to initialize the property in a constructor of the class. Here is an example of the solution:

using static System.Console;

StaffPayroll pay = new();

pay.EmployeeNumber = 280_735;


pay.EmployeeName[0] = "Michael";
pay.EmployeeName[1] = "Simms";

pay.TimeWorked[0] = 6.00;
pay.TimeWorked[1] = 8.50;
pay.TimeWorked[2] = 7.00;
pay.TimeWorked[3] = 5.50;
pay.TimeWorked[4] = 6.50;

WriteLine("Payroll Summary");
WriteLine("-----------------------------------");
WriteLine("Employee #:          {0}", pay.EmployeeNumber);
WriteLine("Employee Name:       {0} {1}", pay.EmployeeName[0], pay.EmployeeName[1]);
WriteLine("===================================");
WriteLine("Time Worked");
WriteLine("-----------------------------------");
WriteLine("     Monday:         {0:N}", pay.TimeWorked[0]);
WriteLine("     Tuesday:        {0:N}", pay.TimeWorked[1]);
WriteLine("     Wednesday:      {0:N}", pay.TimeWorked[2]);
WriteLine("     Thursday:       {0:N}", pay.TimeWorked[3]);
WriteLine("     Friday:         {0:N}", pay.TimeWorked[4]);
WriteLine("-----------------------------------");
WriteLine("Total Time Worked:   {0:N}",
          pay.TimeWorked[0] + pay.TimeWorked[1] +
          pay.TimeWorked[2] + pay.TimeWorked[3] + pay.TimeWorked[4]);
WriteLine("====================================");

internal record StaffPayroll
{
    int nbr;
    string[] name;

    double[] times;

    public Payroll()
    {
        name = new string[2];
        times = new double[5];
    }

    public int EmployeeNumber
    {
        get { return nbr; }
        set { nbr = value; }
    }

    public string[] EmployeeName
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }

    public double[] TimeWorked
    {
        get { return times; }
        set { times = value; }
    }
}

Another and in fact better solution is to apply the null conditional operator on the property. If you are creating a complete property, you should apply the operator on both the property and its corresponding field. This can be done as follows:

using static System.Console;

StaffPayroll pay = new();

pay.EmployeeNumber = 280_735;

pay.EmployeeName = new string[2];
pay.EmployeeName[0] = "Michael";
pay.EmployeeName[1] = "Simms";

pay.TimeWorked = new double[5];
pay.TimeWorked[0] = 6.00;
pay.TimeWorked[1] = 8.50;
pay.TimeWorked[2] = 7.00;
pay.TimeWorked[3] = 5.50;
pay.TimeWorked[4] = 6.50;

WriteLine("Payroll Summary");
WriteLine("-----------------------------------");
WriteLine("Employee #:          {0}", pay.EmployeeNumber);
WriteLine("Employee Name:       {0} {1}", pay.EmployeeName[0], pay.EmployeeName[1]);
WriteLine("===================================");
WriteLine("Time Worked");
WriteLine("-----------------------------------");
WriteLine("     Monday:         {0:N}", pay.TimeWorked[0]);
WriteLine("     Tuesday:        {0:N}", pay.TimeWorked[1]);
WriteLine("     Wednesday:      {0:N}", pay.TimeWorked[2]);
WriteLine("     Thursday:       {0:N}", pay.TimeWorked[3]);
WriteLine("     Friday:         {0:N}", pay.TimeWorked[4]);
WriteLine("-----------------------------------");
WriteLine("Total Time Worked:   {0:N}",
          pay.TimeWorked[0] + pay.TimeWorked[1] +
          pay.TimeWorked[2] + pay.TimeWorked[3] + pay.TimeWorked[4]);
WriteLine("====================================");

internal class StaffPayroll
{
    int nbr;
    string[]? name;
    double[]? times;

    public int EmployeeNumber
    {
        get { return nbr; }
        set { nbr = value; }
    }

    public string[]? EmployeeName
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }

    public double[]? TimeWorked
    {
        get { return times; }
        set { times = value; }
    }
}

In the same way, if the property is automatic, you can apply the null operator after the square brackets of its type. Here is an example:

internal class StaffPayroll
{
    int nbr;
    string[]? name;
    double[]? times;

    public int EmployeeNumber
    {
        get { return nbr; }
        set { nbr = value; }
    }

    public string[]? EmployeeName
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }

    public double[]? Week1TimeWorked
    {
        get { return times; }
        set { times = value; }
    }

    public double[]? Week2TimeWorked { get; set; }
}

To access the members of the array, if you had used a constructor to initialize the property, access the variable of the class, add a period and the name of the property. This is followed by square brackets in which you put the index of the property element you want to access. Between the name of the property and the opening curly bracket, write the question mark null operator. Here are examples:

using static System.Console;

int nbr = 280_735;

string[] name = new string[2];
name[0] = "Michael";
name[1] = "Simms";

double[] times = new double[5];
times[0] = 6.00;
times[1] = 8.50;
times[2] = 7.00;
times[3] = 5.50;
times[4] = 6.50;

Payroll pay = new(nbr, name, times);

WriteLine("Payroll Summary");
WriteLine("-----------------------------------");
WriteLine("Employee #:          {0}", pay.EmployeeNumber);
WriteLine("Employee Name:       {0} {1}", pay.EmployeeName?[0], pay.EmployeeName?[1]);
WriteLine("===================================");
WriteLine("Time Worked");
WriteLine("-----------------------------------");
WriteLine("     Monday:         {0:N}", pay.TimeWorked?[0]);
WriteLine("     Tuesday:        {0:N}", pay.TimeWorked?[1]);
WriteLine("     Wednesday:      {0:N}", pay.TimeWorked?[2]);
WriteLine("     Thursday:       {0:N}", pay.TimeWorked?[3]);
WriteLine("     Friday:         {0:N}", pay.TimeWorked?[4]);
WriteLine("-----------------------------------");
WriteLine("Total Time Worked:   {0:N}",
          pay.TimeWorked?[0] + pay.TimeWorked?[1] +
          pay.TimeWorked?[2] + pay.TimeWorked?[3] + pay.TimeWorked?[4]);
WriteLine("====================================");

internal record StaffPayroll
{
    int nbr;
    string[]? name;
    double[]? times;

    public Payroll(int emplNbr, string[]? emplName, double[]? tmWorked)
    {
        nbr = emplNbr;
        name = emplName;
        times = tmWorked;
    }

    public int EmployeeNumber
    {
        get { return nbr; }
        set { nbr = value; }
    }

    public string[]? EmployeeName
    {
        get { return name;  }
        set { name = value; }
    }

    public double[]? TimeWorked
    {
        get { return times; }
        set { times = value; }
    }
}

Previous Copyright © 2001-2024, FunctionX Thursday 02 June 2022 Next