21.3 Enum members

原创 2006年06月22日 03:17:00
The body of an enum type declaration defines zero or more enum members,
which are the named constants
of the enum type. No two enum members can have the same name.
enum-member-declarations , enum-member-declaration
attributesopt identifier
attributesopt identifier = constant-expression
Each enum member has an associated constant value. The type of this value is
the underlying type for the
containing enum. The constant value for each enum member must be in the
range of the underlying type for
the enum. [Example: The example
enum Color: uint
Red = -1,
Green = -2,
Blue = -3
results in a compile-time error because the constant values -1, -2, and ?3
are not in the range of the
underlying integral type uint. end example]
Multiple enum members may share the same associated value. [Example: The
enum Color
Max = Blue
shows an enum that has two enum members?Blue and Max?that have the same
associated value. end
The associated value of an enum member is assigned either implicitly or
explicitly. If the declaration of the
enum member has a constant-expression initializer, the value of that
constant expression, implicitly
converted to the underlying type of the enum, is the associated value of
the enum member. If the declaration
of the enum member has no initializer, its associated value is set
implicitly, as follows:
? If the enum member is the first enum member declared in the enum type,
its associated value is zero.
? Otherwise, the associated value of the enum member is obtained by
increasing the associated value of
the textually preceding enum member by one. This increased value must be
within the range of values
that can be represented by the underlying type.
Chapter 21 Enums
[Example: The example
using System;
enum Color
Green = 10,
class Test
static void Main() {
static string StringFromColor(Color c) {
switch (c) {
case Color.Red:
return String.Format("Red = {0}", (int) c);
case Color.Green:
return String.Format("Green = {0}", (int) c);
case Color.Blue:
return String.Format("Blue = {0}", (int) c);
return "Invalid color";
prints out the enum member names and their associated values. The output is:
Red = 0
Green = 10
Blue = 11
for the following reasons:
? the enum member Red is automatically assigned the value zero (since it
has no initializer and is the first
enum member);
? the enum member Green is explicitly given the value 10;
? and the enum member Blue is automatically assigned the value one greater
than the member that
textually precedes it.
end example]
The associated value of an enum member may not, directly or indirectly, use
the value of its own associated
enum member. Other than this circularity restriction, enum member
initializers may freely refer to other
enum member initializers, regardless of their textual position. Within an
enum member initializer, values of
other enum members are always treated as having the type of their
underlying type, so that casts are not
necessary when referring to other enum members.
[Example: The example
enum Circular
A = B,
results in a compile-time error because the declarations of A and B are
circular. A depends on B explicitly,
and B depends on A implicitly. end example]
Enum members are named and scoped in a manner exactly analogous to fields
within classes. The scope of
an enum member is the body of its containing enum type. Within that scope,
enum members can be referred
to by their simple name. From all other code, the name of an enum member
must be qualified with the name
of its enum type. Enum members do not have any declared accessibility?an
enum member is accessible if
its containing enum type is accessible.

《算法导论》笔记 第21章 21.3 不相交集合森林

【笔记】 【练习】
  • cyendra
  • cyendra
  • 2014年04月30日 21:27
  • 646

tomcat - 报错 No such device; No faulty members identified.

启动tomcat集群的时候出现以下报错,导致集群没办法启动: java.net.SocketException: No such device; No faulty members identifi...
  • u010229420
  • u010229420
  • 2016年07月24日 18:04
  • 1563


1. 改变全局数据库名 SQL> conn sys/orcl@demo as sysdba 已连接。 SQL> alter database rename global_name to...
  • yiwuya
  • yiwuya
  • 2009年02月20日 18:38
  • 441


前言这一篇是看过lifetragedy的两篇系列的Apache ActiveMQ实战(2)-集群之后,所写。 主要是写过程中,遇到的问题,和简要的总结。关于ActiveMQ集群当我们的系统庞大之后,...
  • BuquTianya
  • BuquTianya
  • 2016年08月28日 16:18
  • 920

【Nginx+Tomcat+Session 高性能群集搭建】

后期需要在linux系统下搭建环境,感觉文章不错,转来备用一下。 随着IT行业的发展,linux服务器在企业中应用广泛,人们对linux上的应用服务要求也越来越高,早先的apache服务器、...
  • ni_hao_ya
  • ni_hao_ya
  • 2013年07月19日 13:53
  • 1734

C++ VS C#(2):字符串,命名空间

  • norains
  • norains
  • 2010年11月30日 09:37
  • 4852

MDX children与members的误区

  • bendanlzh
  • bendanlzh
  • 2009年07月01日 12:40
  • 2097

hihocoder #1343 : Stable Members(支配树)

时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Recently Little Hi joined an algorithm learni...
  • hexianhao
  • hexianhao
  • 2016年09月05日 17:59
  • 569

Bugzulla TestRunner安装心得

Bugzulla TestRunner安装心得: OS平台:windows2000Bugzilla版本:2.20.2 / 2.20.1   现在未测试2.20(可能在该版本不会出现我所说的问题)Tes...
  • shiqiang1234
  • shiqiang1234
  • 2006年04月30日 10:22
  • 2074

HDU 2297 半平面交

Run Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub...
  • u012358934
  • u012358934
  • 2014年05月15日 17:28
  • 1236
您举报文章:21.3 Enum members