Constants

Variables are, not surprisingly, variable. They can be written to many times, causing the contents of the cell to get replaced. Constants, however, 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//        or indexer.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. ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.