C# Constants
Constants
If you don't want others (or yourself) to overwrite existing values, you can add the const
keyword in front of the variable type.
This will declare the variable as "constant", which means unchangeable and read-only:
The const
keyword is useful when you want a variable to always store the same value so that others (or yourself) won't mess up your code. An example that is often referred to as a constant is PI (3.14159...).
Note: You cannot declare a constant variable without assigning the value. If you do, an error will occur: A const field requires a value to be provided .
An anecdote about C# programmers
Two C# programmers are talking:
— Do you know what is the worst thing about programming in C#?
— No, what?
— When you think you've written the perfect code, and then it turns out that Microsoft has already added this feature in the latest .NET Framework update!