Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 19 / 68

Constants (const)

In this lesson you will learn how to declare constants with const in C#, so values that must not change are handled safely. This is for anyone searching for "C# const how to use".

The const keyword creates a constant whose value cannot be changed later.

The sample code defines a perfect score as a constant with const int MaxScore = 100 and uses it to build a message. Because a value declared with const is checked at compile time, an accidental reassignment is caught before the program ever runs.

A common stumbling block for beginners is choosing between const and readonly. The similar-looking readonly keyword is used when you want a read-only value that differs per instance.

In real-world development, constants are a basic technique for avoiding magic numbers and communicating the intent of your code.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)