Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 17 Classesprevious at this levelnext at this level 17.3 Constants Paragraph 11 A constant is a class member that represents a constant value: a value that can be computed at compile-time. 2 A constant-declaration introduces one or more constants of a given type. constant-declaration : attributesopt constant-modifiersopt const type constant-declarators ; constant-modifiers : constant-modifier constant-modifiers constant-modifier constant-modifier : new public protected internal private constant-declarators : constant-declarator constant-declarators , constant-declarator constant-declarator : identifier = constant-expression Paragraph 21 A constant-declaration may include a set of attributes (§24), a new modifier (§17.2.2), and a valid combination of the four access modifiers (§17.2.3). 2 The attributes and modifiers apply to all of the members declared by the constant-declaration. 3 Even though constants are considered static members, a constant-declaration neither requires nor allows a static modifier. 4 It is an error for the same modifier to appear multiple times in a constant declaration. Paragraph 31 The type of a constant-declaration specifies the type of the members introduced by the declaration. 2 The type is followed by a list of constant-declarators, each of which introduces a new member. 3 A constant-declarator consists of an identifier that names themember, followed by an "=" token, followed by a constant-expression (§14.15) that gives the value of the member. Paragraph 41 The type specified in a constant declaration must be sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, an enum-type, or a reference-type. 2 Each constant-expression must yield a value of the target type or of a type that can be converted to the target type by an implicit conversion (§13.1). Paragraph 51 The type of a constant must be at least as accessible as the constant itself (§10.5.4). Paragraph 61 The value of a constant is obtained in an expression using a simple-name (§14.5.2) or a member-access (§14.5.4). Paragraph 71 A constant can itself participate in a constant-expression. 2 Thus, a constant may be used in any construct that requires a constant-expression. [Note: Examples of such constructs include case labels, goto case statements, enum member declarations, attributes, and other constant declarations. end note] [Note: As described in §14.15, a constant-expression is an expression that can be fully evaluated at compile-time. Since the only way to create a non-null value of a reference-type other than string is to apply the new operator, and since the new operator is not permitted in a constant-expression, the only possible value for constants of reference-types other than string is null. end note] Paragraph 81 When a symbolic name for a constant value is desired, but when the type of that value is not permitted in a constant declaration, or when the value cannot be computed at compile-time by a constant-expression, a readonly field (§17.4.2) may be used instead. [Note: The versioning semantics of const and readonly differ (§17.4.2.2). end note] Paragraph 91 A constant declaration that declares multiple constants is equivalent to multiple declarations of single constants with the same attributes, modifiers, and type. [Example: For example
class A  
{  
   public const double X = 1.0, Y = 2.0, Z = 3.0;  
}  
is equivalent to
class A  
{  
   public const double X = 1.0;  
   public const double Y = 2.0;  
   public const double Z = 3.0;  
}  
end example]
Paragraph 101 Constants are permitted to depend on other constants within the same program as long as the dependencies are not of a circular nature. 2 The compiler automatically arranges to evaluate the constant declarations in the appropriate order. [Example: In the example
class A  
{  
   public const int X = B.Z + 1;  
   public const int Y = 10;  
}  
class B  
{  
   public const int Z = A.Y + 1;  
}  
the compiler first evaluates A.Y, then evaluates B.Z, and finally evaluates A.X, producing the values 10, 11, and 12. end example]
3 Constant declarations may depend on constants from other programs, but such dependencies are only possible in one direction. [Example: Referring to the example above, if A and B were declared in separate programs, it would be possible for A.X to depend on B.Z, but B.Z could then not simultaneously depend on A.Y. end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS