Instance Fields

One of the key aspects of object-oriented design is the grouping of data to provide structure. This section discusses how to add data to the Employee class. The general object-oriented term for a variable that stores data within a class is member variable. This term is well understood in C#, but the more standard term—and the one used in the specification—is field, which is a named unit of storage associated with the containing type. Instance fields are variables declared at the class level to store data associated with an object. Hence, association is the relationship between the field data type and the containing field.

Declaring an Instance Field

In Listing 6.4, the class Employee has been modified to include three fields: FirstName, LastName, and Salary.

Listing 6.4: Declaring Fields
public class Employee
    public string FirstName;
    public string LastName;
    public string? Salary;

With these fields added, it is possible to store some fundamental data with every Employee instance. In this case, you prefix the fields with an access modifier of public. The use of public on a field indicates that the data within the field is accessible from classes other than Employee (see the “Access Modifiers” section later in this chapter).

Like a local variable declaration, a field declaration includes the data type to which the field refers. Furthermore, it is possible to assign fields an initial value at declaration time, as demonstrated with the Salary field in Listing 6.5.

Listing 6.5: Setting Initial Values of Fields at Declaration Time
// Non-nullable field uninitialized warning disabled while code is incomplete
#pragma warning disable CS8618
public class Employee
    public string FirstName;
    public string LastName;
    public string? Salary = "Not enough";

We delay the guidelines of naming and coding fields until later in the chapter, after C# properties and constructors have been introduced. Suffice it to say, until then the listings frequently do not follow the standard coding guidelines. In fact, there is a preponderance of the following warnings:

“CS0649: Field is never assigned to, and will always have its default value null.”
“CS8618: Non-nullable field is uninitialized. Consider declaring as nullable.”

In this case, since FirstName and LastName are not initialized, they trigger the CS8618 warning.

For purposes of elucidation, these warnings are ignored and, in fact, disabled with #pragma directives in the accompanying source code until the concepts are fully developed later in the chapter.

Accessing an Instance Field

You can set and retrieve the data within fields. However, the fact that a field does not include a static modifier indicates that it is an instance field. You can access an instance field only from an instance of the containing class (an object). You cannot access it from the class directly (without first creating an instance, in other words).

Listing 6.6 shows an updated look at the Program class and its utilization of the Employee class, and Output 6.1 shows the results.

Listing 6.6: Accessing Fields
public class Program
    public static void Main()
        Employee employee1 = new();
        Employee employee2;
        employee2 = new Employee();
        employee1.FirstName = "Inigo";
        employee1.LastName = "Montoya";
        employee1.Salary = "Too Little";
            employee1.FirstName } 
            employee1.LastName }
            employee1.Salary }");
        // ...
    public static void IncreaseSalary(Employee employee)
        employee.Salary = "Enough to survive on";
Output 6.1
Inigo Montoya: Enough to survive on

Listing 6.6 instantiates two Employee objects, as you saw earlier. It then sets each field, calls IncreaseSalary() to change the salary, and displays each field associated with the object referenced by employee1.

Notice that you first have to specify which Employee instance you are working with. Therefore, the employee1 variable appears as a prefix to the field name when assigning and accessing the field.

{{ snackbarMessage }}