C Sharp Display Variables

C# Display Variables – Complete Beginner Guide
Understanding how to display variables in C# is an essential skill for every beginner.
Variables store data, but displaying them allows users to see results, outputs, and program responses. Whether you’re building a console application, learning basics, or debugging your code, knowing how to display variables correctly is crucial.
In this complete beginner guide, you’ll learn:
What displaying variables means
How to print variables using Console
Console.Write vs Console.WriteLine
String concatenation
String interpolation (recommended method)
Composite formatting
Formatting numbers
Escape characters
Common mistakes
Best practices
Let’s get started
What Does “Display Variables” Mean in C#?
Displaying variables means showing the value stored inside a variable on the screen.
In C#, this is typically done using the Console class from the System namespace.
Before displaying variables, make sure you include:
1 | using System; |
Basic Example of Displaying a Variable
1 2 | int age = 25; Console.WriteLine(age); |
Output:
Here:
agestores value 25Console.WriteLine(age)prints the value
Simple and beginner-friendly.
Using Console.WriteLine() to Display Variables
Console.WriteLine() prints output and moves the cursor to the next line.
Example:
1 2 | string name = "Sanjit"; Console.WriteLine(name); |
Output:
Using Console.Write() to Display Variables
Console.Write() prints output without moving to the next line.
Example:
1 2 3 | int number = 10; Console.Write(number); Console.Write(number); |
Output:
Difference:
Write → stays on same line
WriteLine → moves to next line
Displaying Text and Variables Together
Often, you want to display labels with variable values.
Example:
1 2 | int age = 25; Console.WriteLine("Age: " + age); |
Output:
This is called string concatenation.
String Concatenation in C#
Concatenation means joining strings using +.
Example:
1 2 3 4 | string firstName = "Sanjit"; string lastName = "Sinha"; Console.WriteLine("Full Name: " + firstName + " " + lastName); |
Output:
While this works, modern C# offers better ways.
String Interpolation (Recommended Method)
String interpolation is the cleanest and most professional way to display variables.
Syntax:
1 | Console.WriteLine($"Text {variable}"); |
Example:
1 2 | int age = 25; Console.WriteLine($"Age: {age}"); |
Output:
Advantages:
- Cleaner
- Easier to read
- Less error-prone
- Professional standard
In 2026, this is the preferred method.
Displaying Multiple Variables
You can display multiple variables in one statement.
Example:
1 2 3 4 | string name = "Sanjit"; int age = 25; Console.WriteLine($"Name: {name}, Age: {age}"); |
Output:
Composite Formatting Method
Another way to display variables:
1 | Console.WriteLine("Name: {0}, Age: {1}", name, age); |
Here:
{0}refers to first variable{1}refers to second variable
Although older, it’s still used in some projects.
Displaying Numeric Variables with Formatting
You can format numbers while displaying them.
Example:
1 2 | double price = 1234.5678; Console.WriteLine(price.ToString("F2")); |
Output:
Currency Format
1 | Console.WriteLine(price.ToString("C")); |
Percentage Format
1 2 | double discount = 0.25; Console.WriteLine(discount.ToString("P")); |
Formatting improves presentation quality.
Displaying Boolean Variables
Example:
1 2 | bool isStudent = true; Console.WriteLine($"Student Status: {isStudent}"); |
Output:
Using Escape Characters When Displaying Variables
Escape characters help format output.
| Escape | Meaning |
|---|---|
| \n | New line |
| \t | Tab |
| “ | Double quote |
Example:
1 2 | string name = "Sanjit"; Console.WriteLine($"Hello\n{name}"); |
Output:
Sanjit
Real Beginner Program Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System; class Program { static void Main() { string name = "Sanjit"; int age = 20; double salary = 15000.50; bool isEmployed = true; Console.WriteLine("User Details:"); Console.WriteLine($"Name: {name}"); Console.WriteLine($"Age: {age}"); Console.WriteLine($"Salary: {salary:C}"); Console.WriteLine($"Employed: {isEmployed}"); } } |
This demonstrates:
Multiple variable types
String interpolation
Currency formatting
Clean output structure
Common Beginner Mistakes
Forgetting $ in interpolation
1 | Console.WriteLine("Age: {age}"); |
Correct:
1 | Console.WriteLine($"Age: {age}"); |
- Missing semicolon
- Wrong capitalization
- Mixing data types incorrectly
Best Practices for Displaying Variables
- Use string interpolation
- Format numbers properly
- Use meaningful labels
- Keep output readable
- Avoid messy concatenation
- Use WriteLine for clarity
Why Displaying Variables Is Important
Displaying variables helps you:
Show results
Debug programs
Interact with users
Build interactive applications
Present reports clearly
Every real-world application uses variable display in some form.
Frequently Asked Questions (FAQs)
1. How do you display a variable in C#?
You use Console.WriteLine() or Console.Write() to display the value stored in a variable.
2. What is the best way to display variables?
String interpolation using $" " is the recommended and modern approach.
3. Can I display multiple variables at once?
Yes. You can use interpolation or composite formatting.
4. How do I format numbers while displaying them?
Use ToString() with format specifiers like F2, C, or P.
5. What is the difference between Write and WriteLine?
Write does not move to a new line. WriteLine moves to the next line after printing.
Final Thoughts
Displaying variables in C# is:
Simple
Essential
Beginner-friendly
Powerful
Mastering variable display helps you:
Understand program output
Build interactive applications
Improve debugging skills
Write professional console programs
Once you understand how to display variables clearly, your C# programs become much more dynamic and user-friendly.
