Menu
Coddy logo textTech

Constants

Part of the Fundamentals section of Coddy's C# journey — lesson 11 of 69.

A constant is a special type of variable whose value cannot be changed once it is initialized. To declare a constant, use the const keyword followed by the data type and the constant name:

const int MAX_VALUE = 100;
const double PI = 3.14159;
const string MESSAGE = "Hello, Coddy!";

In these examples, MAX_VALUE, PI, and MESSAGE are constants that cannot be modified after initialization. Attempting to change their values will result in a compilation error.

challenge icon

Challenge

Beginner

Write a program that declares and initializes the following constants:

  • A constant named MAX_QUANTITY with the value 50.
  • A constant named COMPANY_NAME with the value "Coddy Inc.".

Cheat sheet

A constant is a special type of variable whose value cannot be changed once it is initialized. Use the const keyword followed by the data type and constant name:

const int MAX_VALUE = 100;
const double PI = 3.14159;
const string MESSAGE = "Hello, Coddy!";

Constants cannot be modified after initialization - attempting to change their values will result in a compilation error.

Try it yourself

using System;

public class Program {
    public static void Main(string[] args) {
        // Declare constants here
        
        
        // Don't change below
        Console.WriteLine("Max Quantity: " + MAX_QUANTITY);
        Console.WriteLine("Company Name: " + COMPANY_NAME);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals