readonly Fields

 
Chapter 3 - Object-Oriented C#
bySimon Robinsonet al.
Wrox Press 2002
  

The concept of a constant, as a variable that contains a value that cannot be changed, is something that C# shares with most programming languages. However, constants don't necessarily meet all requirements. On occasions, you may have some variable whose value shouldn't be changed, but where the value is not known until runtime. C# provides another type of variable that is useful in this scenario: the readonly field.

The readonly keyword gives a bit more flexibility than const , allowing for the case in which you might want a field to be constant but also need to carry out some calculations to determine its initial value. The rule is that you can assign values to a readonly field inside a constructor, but not anywhere else. It's also possible for a readonly field to be an instance rather than a static field, having a different value for each instance of a class. This means that, unlike a const field, if you want a readonly field to be static, you have to explicitly declare it as such.

Suppose we have an MDI program that edits documents, and for licensing reasons we want to restrict the number of documents that can be opened simultaneously . But, because we are selling different versions of the software, and it's possible that customers can upgrade their licenses, we can't hard-code the maximum number in the sourcecode. We'd probably need a field to represent this maximum number. This field will have to be read in - perhaps from a registry key or some other file storage - each time the program is launched. So our code might look something like this:

 public class DocumentEditor    {   public static readonly uint MaxDocuments;   static DocumentEditor()       {   // code here will read in the value of the max no. of documents.     // for the sake of argument, let's assume the result is 20     MaxDocuments = 20;   } 

In this case, the field is static, since the maximum number of documents only needs to be stored once per running instance of the program. This is why it is initialized in the static constructor. If we had an instance readonly field then we would initialize it in the instance constructor(s). For example, presumably each document we edit has a creation date, which you wouldn't want to allow the user to ever change (because that would be rewriting the past!). Note that the field is also public - we don't need to make readonly fields private, because by definition they cannot be modified externally (the same principle applies to constants too).

A date is represented by the base class, System.DateTime , which we've already encountered briefly . In this code we use a System.DateTime constructor that takes three parameters (the year, month, and day of the month):

 public class Document    {       public readonly DateTime CreationDate;       public Document()       {   // read in creation date from file. Assume result is 1 Jan 2002     // but in general this can be different for different instances     // of the class     CreationDate = new DateTime(2002, 1, 1);   }       } 

CreationDate and MaxDocuments in the above code snippets are treated like any other field, except that because they are readonly, it cannot be assigned to outside the constructors.

   void SomeMethod()     {     MaxDocuments = 10;    // compilation error here. MaxDocuments is readonly     }   

It's also worth noting that you don't have to assign a value to a readonly field in a constructor. If you don't it will be left with the default value for its particular data type or whatever value you initialized it to at its declaration. That applies to both static and instance readonly fields.

  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net