Home » C# Basics » 03 - C# Programming - Part 1
3
Constants
Defines what constants are and how it is used in a C# program
If the keyword `const' is added in a variable declaration, the variable will be treated as a constant. Now 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;
the code will generate an error.
Also note that the value in the constant needs to be assigned when it needs to be declared:
const int number=10;
If we do it like a variable,
const int number; number=10;
this will also result in error.