Constants
Variables are, not surprisingly, variable. They can be written to many times, causing the contents of the cell to get replaced. Constants, on the other hand, cannot be changed after their initialization, which needs to happen at the point the constant is declared. To declare a local constant, use the const keyword:
const int x = 42;// Error: The left-hand side of an assignment must be a variable, property orindexer.x = 43;
Constants are subject to restrictions on what they can be initialized to: The value needs to be computable at compile time. In practice, this means the integral and real number, character, string, and Boolean types are allowed, as well as reference types when the null literal is used.
It’s educational to see what’s ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access