Menu

keywords-const

Will Pittenger

The const keyword can be used in several different ways.

  • As a class modifier
  • To declare a run-time constant
  • To declare that what a reference points to can't be changed
  • To declare that a method won't change the any data members or call anything that could change a data member.

Modifying class declarations

When you declare a class as constant, all non-static data members become both const and readonly. All non-static methods become const and non-static properties are limited to get. const can't be combined with static when used as a class modifier. const goes before the class keyword. const classes are effectively immutable.

All operators are already const and can be used with any type declared const.

[(\static\ | \const\)] \class\ /identifier/
  ' Declare nested-types, nested type-less named structures, data members, constructors, static constructors, creators, static creators, destructors, methods, and properties as needed

Declaring a run-time constant

These are similar to conditional variables, except these are associated with a class and can't be used inside conditionalif statements. Variables declared this way are both const (as described below and) readonly. These variables are automatically static and not part of any one instance of the enclosing type.

\const\ /TypeDescriptor/ /Identifier/ \=\ /Value/

/TypeDescriptor/ must be a type descriptor as described in Type descriptors. /Value/ must be an R-Value.

Declaring a constant variable that isn't readonly

If you combine const with var (in either order), you get a constant variable that doesn't have readonly applied to it. Such variables can be initialized either when declared or by the constructor. They can be static or non-static. Regardless of how they are declared, they must be initialized by the time the constructor returns. Otherwise, the compiler must issue an error.

(\var\ \const\ | \const\ \var\) /identifier/ [= /DefaultValue/]

Declaring a constant method

If you place const between the access qualifier and the command/function/statement keywords, you're declaring a procedure that doesn't change any non-static data members or call anything that could change a non-static data member. The compiler is required to enforce this rule. const methods can be static, virtual, or **abstract as needed.

In each case, /DeclarationSequence/ must be as described in Declaration sequence. /TypeDescriptor/ must be as described in Type descriptors.

Commands

(\public\ | \protected\ | \private\) [\const\] [\static\] | [(\virtual\ | \abstract\)] \command\ [/DeclarationSequence/]
    [\throws\ /TypeDescriptor/]
  ' Run some code here

Functions

(\public\ | \protected\ | \private\) [\const\] [\static\] | [(\virtual\ | \abstract\)] \command\ [/DeclarationSequence/]
    (\returns\ /TypeDescriptor/ |
    [\throws\ TypeDescriptor])
  ' Run some code here

Complex statements

(\public\ | \protected\ | \private\) [\const\] [\static\] | [(\virtual\ | \abstract\)] \statement\ [/DeclarationSequence/]
    [\throws\ /TypeDescriptor/]
  ' Run some code here
  \instructions\ ' See notes below

If this is a prototype for an interface, abstract class, or for use as a delegate type; instructions must be followed by a list of types that will correspond to the variables made visible by the complex statement. Otherwise, instructions must be followed by the actual variables.

Operators

All operators are already const and don't require use of the const keyword.


Related

Wiki: Appendices-Terms-Type descriptors
Wiki: Keywords
Wiki: keywords-abstract
Wiki: keywords-class
Wiki: keywords-conditional
Wiki: keywords-conditionalif
Wiki: keywords-get
Wiki: keywords-instructions
Wiki: keywords-interface
Wiki: keywords-new
Wiki: keywords-property
Wiki: keywords-readonly
Wiki: keywords-ref
Wiki: keywords-returns
Wiki: keywords-set
Wiki: keywords-static
Wiki: keywords-typealias
Wiki: keywords-var
Wiki: keywords-volatile
Wiki: operators-brackets
Wiki: operators-can-be-null