34,333
edits
Changes
→What is a C# Constant?
== What is a C# Constant? ==
A constant is similar to a variable in that it provide a named location in memory to store a data value. Constants differ in one significant way in that once a value has been assigned to constant it cannot subsequently be changed.
Constants are particularly useful if there is a value which is used repeatedly throughout the application code. Rather than use the value each time, it makes the code easier to read if the value is first assigned to a constant and which is then referenced in the code. For example, it might not be clear to someone reading your C# code why you used the value 5 in an expression. If, instead of the value 5, you use a constant named ''interestRate'' the purpose of the value becomes much clearer. Constants also have the advantage that the if the programmer needs to change a widely used value it only needs to be changed once in the constant declaration and not each time it is referenced.
As with variables, constants have a type, a name and a value. Unlike variables, constants must be initialized at the same time that they are declared and must be prefixed with the ''const'' keyword:
<pre>
</pre>