`
talin2010
  • 浏览: 520779 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

17.3 Constants

阅读更多
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]
分享到:
评论

相关推荐

    Object-Oriented Software Construction 2nd

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

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

    Microsoft Codeview and Utilities User's Guide

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

    Handbook of Fiber Optic Data Communication---part3

    Conclusion 730 References 732 Appendix A Measurement Conversion Tables 735 Appendix B Physical Constants 737 Appendix C Index of Professional Organizations ...

    Handbook of Fiber Optic Data Communication---part2

    Conclusion 730 References 732 Appendix A Measurement Conversion Tables 735 Appendix B Physical Constants 737 Appendix C Index of Professional Organizations ...

    C++标准(Standard for Programming Language C++)

    - **28.5 命名空间std::regex_constants**:介绍了`std::regex_constants`命名空间。 - **28.6 类regex_error**:定义了正则表达式错误类。 - **28.7 类模板regex_traits**:介绍了`regex_traits`类模板。 - **28.8 ...

    ZendFramework中文文档

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

    Ardunio-语法手册

    - **17.3 `shiftOut()`**:移位输出。 - **17.4 `shiftIn()`**:移位输入。 - **17.5 `pulseIn()`**:测量脉冲宽度。 - **十八、时间** - **18.1 `millis()`**:获取自上电以来的毫秒数。 - **18.2 `micros()`*...

    Python Cookbook, 2nd Edition

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

    The Art of Assembly Language Programming

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

    Programming Excel With Vba And .net.chm

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

    Arduino语法手册

    - **17.3 `shiftOut()`**: 通过串行端口发送字节。 - **17.4 `shiftIn()`**: 通过串行端口接收字节。 - **17.5 `pulseIn()`**: 读取脉冲的持续时间。 #### 十八、时间 时间相关的函数。 - **18.1 `millis()`**: ...

    DebuggingWithGDB 6.8-2008

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

    javascript权威指南(第六版)

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

    Arduino-语法手册

    **17.3 shiftOut()** `shiftOut()` 用于通过移位寄存器输出数据。 **17.4 shiftIn()** `shiftIn()` 用于通过移位寄存器读取数据。 **17.5 pulseIn()** `pulseIn()` 用于测量脉冲的宽度。 #### 十八、时间 ...

    Debugging with GDB --2001年5.3

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

    Debugging with GDB --2003年6.0

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

    最新版的DebuggingWithGDB7.05-2010

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

    Google C++ International Standard.pdf

    Contents Contents ii List of Tables x List of Figures xiv 1 Scope 1 2 Normative references 2 3 Terms and definitions 3 4 General principles 7 4.1 Implementation compliance . ....4.2 Structure of this ...

Global site tag (gtag.js) - Google Analytics