Constants

Similar to, yet different from, read-only fields are constants. Constants aren’t fields but deserve treatment in this context because of their similar syntax and to point out some of the fundamental differences.

The goal of constants is to provide a way to declare values that will never change both during the duration of the program until eternity. Examples of constants include e (2.71828...) and Pi (3.14159...). Things that seem constant but deceptively are not include values such as tax percentages.

For example, System.Math defines a couple of public constants:

image

Constants can be computed at compile time and are stored in the metadata ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.