A constant is a class member that represents a constant value: a value that
can be computed at compile-time. 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
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). The attributes and modifiers apply
to all of the members declared by the
constant-declaration. Even though constants are considered static members,
a constant-declaration neither
requires nor allows a static modifier. It is an error for the same modifier
to appear multiple times in a constant
declaration.
The type of a constant-declaration specifies the type of the members
introduced by the declaration. The type is
followed by a list of constant-declarators, each of which introduces a new
member. A constant-declarator
consists of an identifier that names the member, followed by an ?=? token,
followed by a constant-expression
(§14.15) that gives the value of the member.
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. 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).
The type of a constant must be at least as accessible as the constant
itself (§10.5.4).
C# LANGUAGE SPECIFICATION
220
The value of a constant is obtained in an expression using a simple-name (§1
4.5.2) or a member-access (§14.5.4).
A constant can itself participate in a constant-expression. 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]
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]
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]
Constants are permitted to depend on other constants within the same
program as long as the dependencies are
not of a circular nature. 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] 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]
分享到:
相关推荐
17.3CONSTANTS AND TYPES 141 17.4MEASUREMENT FUNCTIONS 144 17.5MEASUREMENT CONTROL FUNCTIONS 157 17.6DATA SETUP FUNCTIONS 160 17.7INFORMATION FUNCTIONS 177 17.8CONFIGURATION FUNCTIONS 179 18PORTING A ...
17.3 Log Service 350 17.3.1 Core Framework Usage of the Log Service 355 17.3.2 Resource Usage of the Log Service 357 18 Exploring the Domain 359 18.1 Application Factory Attributes 360 18.2 ...
17.3 COVARIANCE AND DESCENDANT HIDING 621 17.4 FIRST APPROACHES TO SYSTEM VALIDITY 628 17.5 RELYING ON ANCHORED TYPES 630 17.6 GLOBAL ANALYSIS 633 17.7 BEWARE OF POLYMORPHIC CATCALLS! 636 17.8 AN ...
17.3 Definitions 17.4 Additional definitions 17.5 Method of description (Informative) 17.6 Library-wide requirements 18 Language support library 18.1 General 18.2 Types 18.3 Implementation ...
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...
Section 17.3. Increasing Resolution Section 17.4. Antialiased Stripe Example Section 17.5. Frequency Clamping Section 17.6. Summary Section 17.7. Further Information Chapter 18. Non-Photorealistic ...
17.3 Saving Memory with the CVPACK Utility Chapter 18 Linking for Windows and OS/2 Systems 18.1 Dynamic-Link Libraries 18.2 Linking without an Import Library 18.3 Linking with an Import Library 18.4 ...
Conclusion 730 References 732 Appendix A Measurement Conversion Tables 735 Appendix B Physical Constants 737 Appendix C Index of Professional Organizations ...
Conclusion 730 References 732 Appendix A Measurement Conversion Tables 735 Appendix B Physical Constants 737 Appendix C Index of Professional Organizations ...
微软内部提供的详细描述C#语言结构和使用的文档,想做高级独立资深开发的不可错误,另外,此文档为英文原文版。建议作为平时学习阅读之用 目录: Table of Contents 1. Introduction 1 1.1 Hello world 1 ...
9.6. Constants for General Date Functions 9.6.1. Using Constants 9.6.2. List of All Constants 9.6.3. Self-Defined OUTPUT Formats with ISO 9.6.4. Self-defined OUTPUT formats using PHP's date() ...
Defining Constants Recipe 6.3. Restricting Attribute Setting Recipe 6.4. Chaining Dictionary Lookups Recipe 6.5. Delegating Automatically as an Alternative to Inheritance Recipe 6.6. ...
You are visitor as of October 17, 1996. The Art of Assembly Language Programming <br>Forward Why Would Anyone Learn This Stuff? 1 What's Wrong With Assembly Language 2 What's Right With ...
Table of Contents | Index Programming Excel with VBA and .NET Preface Part I: Learning VBA Chapter 1.... Section 1.1.... Section 1.2.... Section 1.3.... Section 1.4.... Section 1.5.... Section 1.6....
5.15 Defining Constants 5.16 Managing Options 5.17 Implementing a Set Class 5.18 Implementing a Ring Buffer 5.19 Implementing a Collection 5.20 Delegating Messages to Multiple Objects ...
Table of Contents Summary of gdb . . . . . . . . ....Free Software ....Free Software Needs Free Documentation ....Contributors to gdb....1 A Sample gdb Session ....2 Getting In and Out of gdb ....2.1 Invoking gdb ....
11.2 Constants and Scoped Variables 269 11.3 Destructuring Assignment 271 11.4 Iteration 274 11.5 Shorthand Functions 282 11.6 Multiple Catch Clauses 283 11.7 E4X: ECMAScript for XML 283 12. Server-...
6.4 Single-precision constants and variables.............................65 6.5 Double-precision constants and variables............................67 6.6 Type conversion.................................
Table of Contents Summary of GDB . . . . . . . . ....Free software ....Free Software Needs Free Documentation ....Contributors to GDB ....1 A Sample GDB Session ....2 Getting In and Out of GDB ....Invoking GDB ....
Table of Contents Summary of gdb . . . . . . . . ....Free software ....Free Software Needs Free Documentation ....Contributors to gdb....1 A Sample gdb Session ....2 Getting In and Out of gdb ....2.1 Invoking gdb ....