Assigning Multiple Variables
In C#, you can assign values to multiple variables of the same type on a single line. This makes the code more concise and easier to read, especially when initializing several variables of the same type.
Key Concepts
- Multiple variables of the same type can be declared and initialized in a single statement.
- Each variable is separated by a comma.
- This is particularly useful when working with similar values of the same type.
Example of Assigning Multiple Variables
Code Example
// Declare and initialize multiple variables of the same type
int a = 5, b = 10, c = 15;
// Output the values to the console
Console.WriteLine($"a = {a}, b = {b}, c = {c}");
Output:
a = 5, b = 10, c = 15
Code Explanation: The variables a
, b
, and c
are all declared and initialized in a single statement. This shorthand notation makes the code concise.
Output Explanation: The values of a
, b
, and c
are printed to the console as a = 5
, b = 10
, and c = 15
.