Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Declaring Constants | Dealing with Data Types
C# Basics

Declaring ConstantsDeclaring Constants

Constants are similar to variables however their value is always set at the declaration time and cannot be changed later.

They are useful in making the code easier to understand by indicating which values do not and are not supposed to be changed throughout the program. Moreover, it minimizes accidental unwanted changes to the data hence reducing bugs in the code.

When declaring a constant we use a similar syntax to variable declaration but we add the keyword const before it:

cs

main.cs

If we try to modify a constant, the compiler will show an error:

cs

main.cs

question-icon

Complete the code for declaring a constant called `pi` having a value of `3.14`:

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

Sección 2. Capítulo 10
course content

Contenido del Curso

C# Basics

Declaring ConstantsDeclaring Constants

Constants are similar to variables however their value is always set at the declaration time and cannot be changed later.

They are useful in making the code easier to understand by indicating which values do not and are not supposed to be changed throughout the program. Moreover, it minimizes accidental unwanted changes to the data hence reducing bugs in the code.

When declaring a constant we use a similar syntax to variable declaration but we add the keyword const before it:

cs

main.cs

If we try to modify a constant, the compiler will show an error:

cs

main.cs

question-icon

Complete the code for declaring a constant called `pi` having a value of `3.14`:

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

Sección 2. Capítulo 10
some-alt