Declaring variables and constants
A variable is a named location for a value. Its type tells the compiler what kind of value is permitted. For example, an int holds a whole number. You can initialize a variable when declaring it, then assign another compatible value later. A constant instead names a value that the compiler can determine at compile time and that cannot be reassigned.
int copies = 4;copies = copies + 3;System.Console.WriteLine(copies);The expression on the right reads the old value, adds three, and stores seven in copies.
Expected output (reasoned, not executed):
7Declaration, initialization and assignment
The syntax int copies = 4; declares the local variable copies and initializes it with four. The statement copies = 6; assigns a new value to the existing variable. It does not declare it a second time. Names are case-sensitive, so copies and Copies are distinct. Prefer clear camelCase names for locals. A name cannot start with a digit; reserved keywords cannot be used unescaped.
Use a constant for a genuine fixed rule
Write const int booksPerBox = 5; when a compile-time value is truly fixed in this program. Constants require a valid constant expression at declaration and cannot be changed by later assignment. A value obtained from user input or a changing configuration is usually not a constant. Read-only fields and immutable objects are separate concepts taught later.
Choose a type that expresses meaning
A count is normally an integer. A title is text. A variable type is checked before running the program, so assigning quoted text to int is a compile-time error rather than a request to parse that text. Type conversions are covered in the next chapter. A local variable must have a definitely assigned value before it is read.
Examples and walkthroughs
const int booksPerBox = 5;int boxes = 3;int capacity = booksPerBox * boxes;System.Console.WriteLine(capacity);Only boxes and capacity are variables. The fixed rule stays five. Multiplication gives fifteen.
Expected output (reasoned, not executed):
15Intentionally invalid: the compiler must reject assignment to a constant. No output is expected; exact diagnostic is not tested.
Common mistake and correction
Do not write int copies = 4; int copies = 7; in the same scope to update a value. Declare once, then use assignment. Do not use const for a quantity that needs to change. Avoid names that differ only by letter case.
Try it yourself
A shelf begins with 9 copies and receives 4 more. Declare a variable, update it, and display 13. Then use a constant of 6 slots per shelf and 2 shelves to display total capacity 12.
Next: Numeric types and literals. These examples are static study material; actual C# compilation and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Declaring variables and constants — knowledge check
Apply the lesson concepts to the following scenarios.
What does int copies = 4; do?
After int copies = 4; copies = copies + 3; what is copies?
Which is a suitable compile-time constant?
What happens if you assign "7" to a variable declared int?
Which updates an existing local named boxes without declaring it again?