`
talin2010
  • 浏览: 502200 次
  • 性别: 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]
分享到:
评论

相关推荐

    莱卡全站仪接口文档

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

    Wiley.Software.Defined.Radio.The.Software.Communications.Architecture.May.2007

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

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

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

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

    python3.6.5参考手册 chm

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

    OpenGL Shading Language, Second Edition

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

    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# 语言规格说明(English Edition第五版)

    微软内部提供的详细描述C#语言结构和使用的文档,想做高级独立资深开发的不可错误,另外,此文档为英文原文版。建议作为平时学习阅读之用 目录: Table of Contents 1. Introduction 1 1.1 Hello world 1 ...

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

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

    Python Cookbook英文版

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

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

    IDL英文原版(很好的一份IDL教材)

    6.4 Single-precision constants and variables.............................65 6.5 Double-precision constants and variables............................67 6.6 Type conversion.................................

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

Global site tag (gtag.js) - Google Analytics