Display Variables

In C#, the Console.WriteLine() method is used to display the values of variables. String interpolation is often used to embed variables within a string, making the output more readable.

Key Concepts

  • The Console.WriteLine() method is used to print variables to the console.
  • String interpolation (using $"...") allows embedding variables in a string.
  • It is a useful method for displaying variable values and combining them with text.

Example of Displaying Variables

Code Example

// Declare variables
int x = 10;
int y = 20;

// Output variables using string interpolation
Console.WriteLine($"The value of x is {x}, and the value of y is {y}");

Output:

The value of x is 10, and the value of y is 20

Code Explanation: The variables x and y are declared and printed using the Console.WriteLine() method. String interpolation (the $"..." syntax) is used to embed the variable values directly into the output string.

Output Explanation: The values of x and y are printed within the string, resulting in The value of x is 10, and the value of y is 20.