The readonly Member

   


The readonly Member

Like the constant member, (declared with the const keyword) the readonly member is used to represent an unchanging value. But, whereas the value assigned to the constant must be written in the source code, as demonstrated in the following line

 const decimal MassOfElectron = 9.0E-28m; 

the value of the readonly member does not have to be known until the program is running. The value of a readonly member is initialized in a constructor and cannot be altered after that moment. So, while the constant member remains unchanged throughout the lifetime of a compiled program, the readonly member merely stays unchanged during the lifetime of an object.

Syntax Box 13.4 shows how a readonly member is declared. Apart from the readonly keyword, the overall declaration and the meaning of its different parts resemble that of a conventional instance variable declaration.

Syntax Box 13.4 Declaring a readonly Member

 readonly_member_declaration::= [ <Access_modifier> ] [static] readonly <Type> <Identifier> [ =  graphics/ccc.gif<Expression> ]; 

where:

 <Access_modifier>                 ::= private                 ::= public                 ::= protected                 ::= internal                 ::= protected internal 

Notes:

  • The <Access_modifier> has the same meaning as when applied in a conventional instance variable declaration, except that the readonly member can only be accessed to read from, not assign to.

  • The readonly member is not static by default like its constant counterpart, but must explicitly be declared static to achieve this status.

  • The type of a constant member is restricted to one of the predefined types byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, char, bool, enum, or string. In contrast, the <Type> of a readonly member can be any type.

  • You can optionally initialize a readonly member with the assignment operator followed by an expression (shown as [ = <Expression> ] ). However, this does not prevent you from assigning a value to this readonly member in a constructor.

The following example illustrates the use of the readonly member. Usually when a bank account of a real-world bank is opened, a unique account number is assigned to it that must stay unchanged throughout the lifetime of the account. We can reflect this fact in our Account class by declaring the representation of the Account number to be readonly, as in line 5 of Listing 13.3. The instance constructor in lines 7 10 can then initialize this account number, and no further changes are then possible throughout the lifetime of the new object.

Listing 13.3 PermanentAccountNumber.cs
01: using System; 02: 03: class Account 04: { 05:     public readonly string AccountNumber; 06: 07:     public Account (string permanentAccountNumber) 08:     { 09:         AccountNumber = permanentAccountNumber; 10:     } 11: } 12: 13: class SimpleBank 14: { 15:     public static void Main() 16:     { 17:         Account yourAccount = new Account("8487-9873-9938"); 18:         Console.WriteLine("Your account number: " 19:             + yourAccount.AccountNumber); 20:     } 21: } Your account number: 8487-9873-9938 

Notice that AccountNumber is declared public in line 5. We can safely do this without breaking any encapsulation principles because, even though AccountNumber can be accessed directly without the use of an accessor method, its value cannot be changed.

The constant member would be unsuited to represent the account numbers because the account number would not be known at the time the program is compiled.

Note

graphics/common.gif

The conventional capitalization style used for writing readonly identifiers is Pascal Casing.



   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

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