Variables and Standard Types
What is const?
Another type of declaration that is possible in code, is something known as a constant, which is declared using the const keyword. When you declare constant values, they can no longer be changed. Once a constant value is set, it cannot be changed. In the interactive example below, you can see how we have declared a constant integer, and how we can't change its value:
using System; public class Program { ...
Constant variable declarations are the same as the other declarations we've seen, except they are prefixed with the const keyword. Another feature of constant values, is you can only declare constants with literal values. In the example below, we can't set a constant value to a new instance of a class:
const Person person = new Person(); // Won't compile.
Whereas literal values are accepted:
const int one = 1; const string two = "two";
Page 11 of 17