const Statement

Declares a constant.

//Syntax for declaring a constant of global scope or function scope.
const name1 [: type1] = value1 [, ... [, nameN [: typeN] = valueN]]
//Syntax for declaring a constant field in a class.
[modifiers] const name1 [: type1] = value1 [, ... [, nameN [: typeN] = valueN]]

Arguments

  • modifiers
    Optional. Modifiers that control the visibility and behavior of the field.

  • name1, ..., nameN
    Required. The names of the constants being declared.

  • type1, ..., typeN
    Optional. The types of the constants being declared.

  • value1, ..., valueN
    The values assigned to the constants.

Remarks

Use the const statement to declare constants. A constant may be bound to a specific data type to help provide type safety. These constants must be assigned values when they are declared, and these values cannot be changed later in the script.

A constant field in a class is similar to a global or function constant, except that it is scoped to the class and it can have various modifiers governing its visibility and usage.

Note

When a constant is bound to a reference data type (such as an Object, Array, class instance, or typed array), the data referenced by the constant may be changed. This is allowed because the const statement makes only the reference type constant; the data to which it refers is not constant.

Example

The following examples illustrate the use of the const statement.

class CSimple {
   // A static public constant field. It will always be 42.
   static public const constantValue : int = 42;
}
const index = 5;
const name : String = "Thomas Jefferson";
const answer : int = 42, oneThird : float = 1./3.;
const things : Object[] = new Object[50];
things[1] = "thing1";
// Changing data referenced by the constant is allowed.

Requirements

Version .NET

See Also

Reference

var Statement

function Statement

class Statement

Concepts

Scope of Variables and Constants

Type Annotation

Other Resources

Modifiers