C# Classes Explained - Fields
(Page 4 of 6 )
A field is a class level variable, or you can call it a member variable. If you declare a field of a value type, it stores the data itself, but if you declare a field of a reference type, it stores a reference to where the actual data is allocated. You must be careful when you define member variables. Let's take an example:
using System;
namespace MyCompany
{
public class Employee
{
public decimal Salary;
}
public class EmployeeTest
{
static void Main(string[] args)
{
// this is client code that can modify the value of the Employee's salary
Employee Michael = new Employee();
Michael.Salary = 10000;
Console.WriteLine("Michael's salary = {0:C}",Michael.Salary);
Michael.Salary = 0;
Console.WriteLine("Michael's salary = {0:C}",Michael.Salary);
Console.ReadLine();
}
}
The class Employee has only one public field (Salary) which represents the employee's salary. The EmployeeTest class' Main method creates the instance Michael of type Employee, and the next statement sets the salary to $10,000. As you can see, the client code assigns the value to the public field Salary, and you don't have any kind of security over the process. Again, the Main method assigns 0 to the salary and writes it to the Console Window. You will get the following result:

Good Object oriented practice states that you must declare your fields as private, and provide provide public properties to access the fields (we will discuss properties later in the series, too, so don't worry). Let's look at the MSIL code for Employee class. Open the ILDASM tool and load the application, then navigate to the Employee class and double click on the Salary field:

The MSIL is just telling us that it's a public field of type System.Decimal, and it's called Salary. This means that when you instantiate the Employee class, the created object will have a block of memory for the Salary field; that is worth remembering.
With other programming languages we had a very common problem, which C# has vanquished. Suppose that you need a field to be initialized at runtime and never change after that. If you know the value at design time, you can use constants (as we will discuss shortly). You can declare your field with the keyword "readonly" to have this behavior, and once it has been initialized at runtime it will never change. You have only one place to initialize the "readonly" field, which is the class constructor. Turning back to our Employee class, we need a "readonly" field to hold the SSN that will dynamically return from a method; this method maybe will connect to a database server to get the SSN of the employee, so the SSN is not available at design time.
using System;
namespace MyCompany
{
public class Employee
{
public decimal Salary;
public readonly double SSN;
// class constructor
public Employee()
{
SSN = this.GetSSN();
}
private double GetSSN()
{
//we will assume that we have connected to a database
return 987654321;
}
}
public class EmployeeTest
{
static void Main(string[] args)
{
// this is client code that can modify the value of the Employee's salary
Employee Michael = new Employee();
Console.WriteLine(Michael.SSN);
Console.ReadLine();
}
}
}
Next: Constants >>
More C# Articles
More By Michael Youssef