C-SHARP - 2.4 Type and constant exercise

This exercise puts the previous lessons into practice. Goal: print a person's profile to the console — last name, first name, age and bank balance — by storing each field in the appropriate variable or constant. The data is up to you, but the type of each field is dictated by what it represents. Last name and first name do not change over time and are strings: use const string. Age is a whole number that changes over time, so a regular int variable. Bank balance is a number with decimals, so a real-number type.

The corrected solution looks like this. Two string constants for the immutable identity fields: const string nom = "Bellaiche"; and const string prenom = "Jibril";. An int variable for the age: int age = 17;. For the balance, a real number — double, float or decimal all work, but float uses the least memory (4 bytes) which is enough for a simple balance. Remember to append F after the literal, otherwise the compiler treats 3045.5 as a double and refuses the assignment: float solde = 3045.5F;.

Why type choice matters

  • Memory efficiency: a byte or short would also work for age (0-255 is enough), but int is the conventional default.
  • Suffixes: F for float, M for decimal, nothing for double.
  • Const vs var: constants protect values that must never change; regular variables hold values that evolve.

Finally, print each field with Console.WriteLine using string concatenation, for example Console.WriteLine("Nom : " + nom); repeated for first name, age and balance. As a developer you should always pick the lightest type that fits the data — programs that are tight on memory are faster, cheaper to run and kinder to the environment. Lesson over: nail the type choice before you write the first line of logic.

Summary

This lesson teaches you how to apply type and constant theory through a practical exercise: creating a program that displays personal information (name, age, and bank balance) in the console. You'll learn to use the appropriate data types—const strings for unchanging values like name and surname, int variables for age, and float for decimal numbers like bank balance. The lesson emphasizes C# conventions, memory optimization, and proper syntax including the necessary suffixes for float and decimal literals.

Key points

  • Use const keyword with string type for data that never changes (name, surname)
  • Declare variables with int type for numbers that change over time (age)
  • Use float type for decimal numbers (bank balance) to optimize memory consumption compared to double
  • In C#, add suffix 'f' after float literals and 'm' after decimal literals; double requires no suffix
  • Use Console.WriteLine() with string concatenation or interpolation to display variables in the console

FAQ

Why should I use float instead of double for the bank balance?

Float uses only 4 bytes of memory compared to double's 8 bytes, making your program lighter and more efficient. As a developer, you should optimize programs to consume less memory and energy, improving performance and environmental impact.

What happens if I forget the 'f' suffix on a float number in C#?

C# will treat the number as a double by default, and you cannot assign a double value directly to a float variable without the 'f' suffix. This will cause a compilation error.

How do I display multiple variables together using Console.WriteLine?

Use string concatenation with the '+' operator (e.g., "Name: " + nameVariable) or string interpolation to format and output all values to the console on a single line.