C# Multiple Locals

The C# programming language

A single statement can assign multiple local variables. Languages derived from C-style syntax often allow you to use commas to declare multiple variables in one statement. The C# language allows this. But it restricts the type of the variables.

Example

Note

Let's introduce a program text that use the multiple local variable declarator syntax. In this syntax, you can declare and optionally assign several variables in a single statement. The comma character is used to separate the variable declarators. Note that the type of all of the variables declared or assigned this way is the same in a single statement.

This C# example program assigns multiple local variables on one line.

Program that initializes multiple variables [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// Declare and initialize three local variables with same type.
	// ... The type int is used for all three variables.
	//
	int i = 5, y = 10, x = 100;
	Console.WriteLine("{0} {1} {2}", i, y, x);
	//
	// Declare and initialize three constant strings.
	//
	const string s = "dot", a = "net", m = "perls";
	Console.WriteLine("{0} {1} {2}", s, a, m);
	//
	// Declare three variables.
	// ... We only initialize one of them at first.
	//
	int j = 1, k, z;
	Console.WriteLine(j);
	k = z = 0; // Initialize the others
	Console.WriteLine("{0} {1}", k, z);
    }
}

Output

5 10 100
dot net perls
1
0 0
Main method

Description. The Main method body contains three local variable declaration lines. The first section declares and assigns three int locals. The second section declares and assigns three constant string literal references, which are actually replaced by their values in their usage locations. The final section declares three local ints but only assigns one of them.

Note: Some programming teams and guidelines discourage this syntax as it is somewhat unusual. In these situations, it should be avoided. It is easier and more consistent to declare and assign one variable on each line instead.

Local variable memory slots

Expert .NET 2.0 IL Assembler

Let's consider how the execution engine, which interprets the abstract assembly code in the intermediate language, allocates memory for local variables. Whenever a method is called, it is pushed onto the stack frame and three different spaces of memory are allocated. All the local variables used throughout the method are allocated in one memory space, while the evaluation stack and parameter slots are allocated separately.

In other words, the count of variables can impact the performance hit when calling a method. An excellent description of this concept is found in Serge Lidin's book Expert .NET IL Assembler.

Expert .NET 2.0 IL Assembler Review

Summary

We looked at the multiple local variable declaration and assignment syntax in the C# programming language. This syntax allows you to condense the syntax of variable declarations in your method bodies. Each statement list of this type can have only a single type name.

Method Tips
.NET