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