Menu
CoddyTech

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 MaxValue = 100;
const double Pi = 3.14159;
const string Message = "Hello, Coddy!";

In these examples, MaxValue, 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 MaxQuantity with the value 50.
  • A constant named CompanyName with the value "Coddy Inc.".

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: " + MaxQuantity);
        Console.WriteLine("Company Name: " + CompanyName);
    }
}
quiz iconTest yourself

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

All lessons in Fundamentals

Practice on your own: Online C# compiler