📖 Scenario: You are creating a simple program to store information about a car. Some details about the car never change, like the number of wheels, while others can be set once when the car is created and then stay the same, like the car's model year.
🎯 Goal: Build a C# program that uses a const field for the number of wheels and a readonly field for the model year. You will see how to set these values and print them.
📋 What You'll Learn
Create a class called Car.
Add a const int field named NumberOfWheels with the value 4.
Add a readonly int field named ModelYear.
Create a constructor for Car that takes an int parameter to set ModelYear.
Create a method DisplayInfo that prints the number of wheels and model year.
In Main, create a Car object with model year 2023 and call DisplayInfo.
💡 Why This Matters
🌍 Real World
Constants and readonly fields are used in programs to keep important values safe from accidental changes, like fixed settings or initial data.
💼 Career
Understanding constants and readonly fields is important for writing reliable and maintainable code in many software development jobs.
Progress0 / 4 steps
1
Create the Car class with a const field
Create a class called Car and inside it, declare a const int field named NumberOfWheels and set it to 4.
C Sharp (C#)
Hint
Use public const int NumberOfWheels = 4; inside the Car class.
2
Add a readonly field and constructor
Inside the Car class, add a readonly int field named ModelYear. Then create a constructor for Car that takes an int modelYear parameter and sets the ModelYear field.
C Sharp (C#)
Hint
Use public readonly int ModelYear; and set it inside the constructor.
3
Add a method to display car info
Inside the Car class, add a method called DisplayInfo that prints the number of wheels and the model year using Console.WriteLine. Use the fields NumberOfWheels and ModelYear.
C Sharp (C#)
Hint
Use Console.WriteLine with the fields inside DisplayInfo.
4
Create a Car object and show info
In the Main method, create a Car object named myCar with model year 2023. Then call myCar.DisplayInfo() to print the details.
C Sharp (C#)
Hint
Create the object with new Car(2023) and call DisplayInfo().
Practice
(1/5)
1. What is the main difference between const and readonly fields in C#?
easy
A. const and readonly are exactly the same and can be used interchangeably.
B. readonly values are set at compile time and cannot change; const values can be set at runtime but only once.
C. const values are set at compile time and cannot change; readonly values can be set at runtime but only once.
D. const values can be changed anytime; readonly values cannot be changed.
Solution
Step 1: Understand const behavior
const fields are fixed at compile time and cannot be changed later.
Step 2: Understand readonly behavior
readonly fields can be assigned once at runtime, usually in a constructor, and then cannot change.
Final Answer:
const values are set at compile time and cannot change; readonly values can be set at runtime but only once. -> Option C
Quick Check:
const fixed at compile, readonly set once at runtime [OK]
Hint: Remember: const fixed early, readonly set once later [OK]
Common Mistakes:
Thinking readonly can be changed multiple times
Confusing when const values are set
Assuming const can be assigned in constructors
2. Which of the following is the correct way to declare a constant integer named MaxValue with value 100 in C#?
easy
A. readonly int MaxValue = 100;
B. const int MaxValue = 100;
C. int const MaxValue = 100;
D. static readonly MaxValue = 100;
Solution
Step 1: Check correct syntax for constant declaration
In C#, const must be followed by type, then name and value, like const int MaxValue = 100;.
Step 2: Identify incorrect options
readonly requires a type and cannot be declared like a constant; int const is invalid syntax in C#; static readonly requires a type and cannot omit it.
Final Answer:
const int MaxValue = 100; -> Option B
Quick Check:
const syntax = const type name = value; [OK]
Hint: const must include type before name [OK]
Common Mistakes:
Using 'int const' instead of 'const int'
Omitting type in readonly declaration
Confusing const and readonly syntax
3. What will be the output of the following C# code?
class Test {
public readonly int Value;
public Test(int val) {
Value = val;
}
}
class Program {
static void Main() {
var t = new Test(10);
Console.WriteLine(t.Value);
}
}
medium
A. 10
B. 0
C. Compilation error
D. Runtime error
Solution
Step 1: Understand readonly assignment in constructor
The readonly field Value is assigned in the constructor with val, which is 10.
Step 2: Check output of Console.WriteLine
The program prints the assigned value of Value, which is 10.
Final Answer:
10 -> Option A
Quick Check:
readonly set in constructor prints assigned value [OK]
Hint: readonly can be set once in constructor, then used [OK]
Common Mistakes:
Assuming readonly defaults to 0 if assigned in constructor
Thinking readonly cannot be assigned in constructor
Expecting compilation or runtime errors
4. Identify the error in the following C# code:
class Sample {
public const int Number;
public Sample() {
Number = 5;
}
}
medium
A. Cannot assign a value to a const field outside declaration.
B. Const fields must be assigned in constructor, not declaration.
C. Const fields can be assigned multiple times.
D. No error, code is correct.
Solution
Step 1: Check const assignment rules
const fields must be assigned a value when declared and cannot be assigned later.
Step 2: Analyze code assignment
The code tries to assign Number = 5; inside the constructor, which is invalid for const.
Final Answer:
Cannot assign a value to a const field outside declaration. -> Option A
Quick Check:
const must be assigned only at declaration [OK]
Hint: const value fixed at declaration, not in constructor [OK]
Common Mistakes:
Trying to assign const in constructor
Confusing const with readonly assignment rules
Assuming no error in code
5. You want to create a class that stores a fixed tax rate that never changes and a discount rate that can be set once when the object is created but never changed after. Which declarations are correct in C#?
hard
A. public double TaxRate = 0.15; public readonly double DiscountRate;
B. public readonly double TaxRate = 0.15; public const double DiscountRate;
C. public static double TaxRate = 0.15; public double DiscountRate;
D. public const double TaxRate = 0.15; public readonly double DiscountRate;
Solution
Step 1: Assign fixed tax rate with const
The tax rate never changes, so const is appropriate for TaxRate.
Step 2: Assign discount rate once at object creation
The discount rate can be set once when the object is created, so readonly is suitable for DiscountRate.
Step 3: Eliminate incorrect options
public readonly double TaxRate = 0.15; public const double DiscountRate; swaps const and readonly incorrectly; public static double TaxRate = 0.15; public double DiscountRate; uses mutable fields; public double TaxRate = 0.15; public readonly double DiscountRate; uses non-const for tax rate.
Final Answer:
public const double TaxRate = 0.15; public readonly double DiscountRate; -> Option D
Quick Check:
const fixed forever, readonly set once at creation [OK]
Hint: Use const for fixed, readonly for one-time set [OK]
Common Mistakes:
Using readonly for fixed values
Using const for values set at runtime
Not understanding when to use static
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.