Home » ASP.NET Basics » 07 - Writing code and handling events
7
Constants
Defining function and uses of constants
If we add the keyword `const' in a variable declaration, the variable will be treated as a constant. Its value cannot be changed in the program. Consider the following:
const int number=10;
Here, we have declared the variable `number' with the keyword `const' and assigned it a value of 10. `number' is now a constant and it will always have the value 10. If we try to change the value with a statement like:
number=20;
our code will generate an error.
Also note that we need to assign the value in the constant when we declare it:
const int number=10;
If we do it like a variable,
const int number; number=10;
this will also result in error.