O'Reilly logo

C# 5.0 Programmer's Reference by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix C Variable Declarations

The following code shows the syntax for declaring a variable inside a method.

«const» type«[]» name «= value»;

The following list describes the pieces of this declaration.

  • const—If you include this, the variable is a constant and its value cannot be changed later. Use the value to assign the constant a value.
  • type—The data type you want the variable to have.
  • []—Include empty square brackets [] to make an array.
  • name—The name you want the variable to have.
  • = value—The value you want the variable to initially have.

C# enables you to declare and initialize more than one variable in a single declaration statement, but this can make the code more difficult to read.

The following code shows the syntax for declaring a variable outside of any method (at the class level).

«attributes» «accessibility»
    «const | readonly | static | volatile | static volatile»
type«[]» name «= value»

The following list describes the pieces of this declaration.

  • attributes—Attributes that specify extra properties for the variable.
  • accessibility—One of public, internal, protected, internal protected, or private. The default is private.
  • const—If you include this, the variable is a constant and its value cannot be changed later. Use the value to assign the constant a value.
  • readonly—If you include this, the variable is similar to a constant except its value can be set either with a value clause or in the class’s constructor.
  • static—This keyword indicates the variable is shared ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required