O'Reilly logo

C# Primer Plus by Klaus Michelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required