C# Display Variables

Welcome to The Coding College, your trusted platform for learning programming concepts! In this tutorial, we will explore how to display variables in C#—a foundational concept for any programmer. You’ll learn how to output variable values using different techniques to make your programs more interactive.

Visit thecodingcollege.com for more beginner-friendly coding tutorials and resources.

Why Display Variables?

Displaying variables is essential in programming to:

  • Debug your program by checking variable values.
  • Interact with users by showing meaningful outputs.
  • Track data changes during program execution.

Basic Output in C#

C# uses the Console.WriteLine() and Console.Write() methods to display variables or text in the console.

  • Console.WriteLine(): Outputs a line of text and moves to the next line.
  • Console.Write(): Outputs text without moving to a new line.

Example:

using System;

class Program
{
    static void Main()
    {
        int age = 24;
        string name = "The Coding College";

        Console.WriteLine("Name: " + name);
        Console.WriteLine("Age: " + age);
    }
}

Output:

Name: The Coding College  
Age: 24

Using String Interpolation

String interpolation is a modern and concise way to include variables in output. It uses the $ symbol before the string and curly braces {} to insert variable values.

Example:

using System;

class Program
{
    static void Main()
    {
        string name = "The Coding College";
        int year = 2024;

        Console.WriteLine($"Welcome to {name}, established in {year}!");
    }
}

Output:

Welcome to The Coding College, established in 2024!  

Combining Strings and Variables

You can concatenate strings and variables using the + operator.

Example:

using System;

class Program
{
    static void Main()
    {
        string name = "C# Learner";
        int lessonsCompleted = 10;

        Console.WriteLine("Hello, " + name + "! You have completed " + lessonsCompleted + " lessons.");
    }
}

Output:

Hello, C# Learner! You have completed 10 lessons.

Displaying Multiple Variables

You can display multiple variables in a single Console.WriteLine() statement using placeholders ({}) and the string.Format() method.

Example with Placeholders:

using System;

class Program
{
    static void Main()
    {
        string name = "The Coding College";
        int year = 2024;
        double rating = 4.9;

        Console.WriteLine("Name: {0}, Year: {1}, Rating: {2}", name, year, rating);
    }
}

Output:

Name: The Coding College, Year: 2024, Rating: 4.9 

Example Program

Here’s a complete program demonstrating various methods to display variables:

using System;

class Program
{
    static void Main()
    {
        // Declare variables
        string studentName = "John Doe";
        int age = 20;
        double gpa = 3.8;

        // Display variables using different methods
        Console.WriteLine("Using concatenation:");
        Console.WriteLine("Name: " + studentName + ", Age: " + age + ", GPA: " + gpa);

        Console.WriteLine("\nUsing placeholders:");
        Console.WriteLine("Name: {0}, Age: {1}, GPA: {2}", studentName, age, gpa);

        Console.WriteLine("\nUsing string interpolation:");
        Console.WriteLine($"Name: {studentName}, Age: {age}, GPA: {gpa}");
    }
}

Output:

Using concatenation:  
Name: John Doe, Age: 20, GPA: 3.8  

Using placeholders:  
Name: John Doe, Age: 20, GPA: 3.8  

Using string interpolation:  
Name: John Doe, Age: 20, GPA: 3.8  

Best Practices for Displaying Variables

  1. Use String Interpolation: It’s modern, readable, and less error-prone.
  2. Organize Outputs: Use consistent formatting to make outputs more professional.
  3. Avoid Hardcoding: Store values in variables for better flexibility.

Real-World Use Case

Suppose you’re building an application for a coding school. You might display the name, number of courses completed, and ratings using variables.

using System;

class Program
{
    static void Main()
    {
        string schoolName = "The Coding College";
        int courses = 25;
        double rating = 4.8;

        Console.WriteLine($"Welcome to {schoolName}! We offer {courses} courses with an average rating of {rating}.");
    }
}

Output:

Welcome to The Coding College! We offer 25 courses with an average rating of 4.8.  

Learn More at The Coding College

Visit thecodingcollege.com for more programming tutorials, coding tips, and resources. Whether you’re just starting or looking to deepen your knowledge, we’ve got you covered.

Conclusion

Displaying variables is a crucial skill in C# programming. With methods like concatenation, placeholders, and string interpolation, you can create dynamic and interactive programs. Start practicing now and see how these techniques can elevate your coding!

Leave a Comment