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
BeginnerWrite a program that declares and initializes the following constants:
- A constant named
MAX_QUANTITYwith the value50. - A constant named
COMPANY_NAMEwith 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);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic Shortcuts5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3