CHAPTER 5 UNDERSTANDING ENCAPSULATION
Notice that you are referencing the constant data defined by MyMathClass using a class name prefix
(i.e., MyMathClass.PI). This is due to the fact that constant fields of a class are implicitly static. However,
it is permissible to define and access a local constant variable within a type member. By way of example:
static void LocalConstStringVariable()
{
// A local constant data point can be directly accessed.
const string fixedStr = "Fixed string Data";
Console.WriteLine(fixedStr);
}
// Error!
fixedStr = "This will not work!";
Regardless of where you define a constant piece of data, the one point to always remember is that
the initial value assigned to the constant must be specified at the time you define the constant. Thus, if
you were to modify your MyMathClass in such a way that the value of PI is assigned in a class constructor
as follows:
class MyMathClass
{
// Try to set PI in ctor?
public const double PI;
}
public MyMathClass()
{
// Error!
PI = 3.14;
}
you would receive a compile-time error. The reason for this restriction has to do with the fact the value
of constant data must be known at compile time. Constructors, as you know, are invoked at runtime.
Understanding Read-Only Fields
Closely related to constant data is the notion of read-only field data (which should not be confused with
a read-only property). Like a constant, a read-only field cannot be changed after the initial assignment.
However, unlike a constant, the value assigned to a read-only field can be determined at runtime and,
therefore, can legally be assigned within the scope of a constructor, but nowhere else.
This can be very helpful when you don’t know the value of a field until runtime, perhaps because
you need to read an external file to obtain the value, but wish to ensure that the value will not change
after that point. For the sake of illustration, assume the following update to MyMathClass:
class MyMathClass
{
// Read-only fields can be assigned in ctors,
// but nowhere else.
public readonly double PI;
public MyMathClass ()
{
PI = 3.14;
208