Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Оголошення Констант | Робота з Типами Даних
Основи C#

bookОголошення Констант

Constants are like variables, but their value is set once at the time of declaration and cannot be changed later.

They are helpful in making the code more understandable by clearly indicating which values are fixed and should not change throughout the program. Additionally, using constants helps prevent accidental changes to data, thereby reducing bugs in the code.

To declare a constant, we use a syntax similar to variable declaration, but we add the keyword const before it:

main.cs

main.cs

copy
12345678910111213
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { const int myVar = 10; Console.WriteLine(myVar); } } }

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

main.cs

main.cs

copy
1234567891011121314
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { const int myVar = 10; myVar = 20; // Error: The left-hand side of an assignment must be a variable, property or indexer. } } }
question-icon

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

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 10

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookОголошення Констант

Свайпніть щоб показати меню

Constants are like variables, but their value is set once at the time of declaration and cannot be changed later.

They are helpful in making the code more understandable by clearly indicating which values are fixed and should not change throughout the program. Additionally, using constants helps prevent accidental changes to data, thereby reducing bugs in the code.

To declare a constant, we use a syntax similar to variable declaration, but we add the keyword const before it:

main.cs

main.cs

copy
12345678910111213
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { const int myVar = 10; Console.WriteLine(myVar); } } }

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

main.cs

main.cs

copy
1234567891011121314
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { const int myVar = 10; myVar = 20; // Error: The left-hand side of an assignment must be a variable, property or indexer. } } }
question-icon

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

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 10
some-alt