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 ...

Get C# 5.0 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.