Local variables - CSharp Language Basics

CSharp examples for Language Basics:Variable

Introduction

The scope of a local variable or local constant extends throughout the current block.

You cannot declare another local variable with the same name in the current block or in any nested blocks.

For example:


int x;
{
  int y;
  int x;            // Error - x already defined
}
{
  int y;            // OK - y not in scope
}
Console.Write (y);  // Error - y is out of scope

Related Tutorials