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
enum-member-declarations , enum-member-declaration
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
example
enum Color
{
Red,
Green,
Blue,
Max = Blue
}
shows an enum that has two enum members?Blue and Max?that have the same
associated value. end
example]
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
295
[Example: The example
using System;
enum Color
{
Red,
Green = 10,
Blue
}
class Test
{
static void Main() {
Console.WriteLine(StringFromColor(Color.Red));
Console.WriteLine(StringFromColor(Color.Green));
Console.WriteLine(StringFromColor(Color.Blue));
}
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);
default:
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,
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]
C# LANGUAGE SPECIFICATION
296
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.
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Android Jni调用浅述

声明:欢迎转载,转载时请注明出处! http://blog.csdn.net/flydream0/article/details/7371692 1 简述     JNI是Java N...

android 中间件开发

JNI的某些数组和字符串类型转换(转) JNICC++C#Windows jbytearray转c++byte数组 C代码   jbyte * arrayBody = env->GetByteAr...

开发Android应用程序,原生开发VS中间件

发现了个写Android程序的工具叫rexsee,在rexsee官网上看着挺牛的,“忘记Java和eclipse,使用HTML和JavaScript开发Android应用程序”。刚开始觉得这玩意儿吹大...

配置安卓中间件的开发环境

对于一个完全没有安卓方面开发经验的小白来说,要在linux系统上搭建安卓的开发环境,只能通过百度,一篇篇的参考论坛大神们的经验了。这几天,鬼知道我经历了什么。接下来就讲讲我如何在安卓开发板上跑起hel...

android NDK 交叉编译

android中间件开发,本来利用NDK就直接生成了一个SO文件,然后直接打包到APK里,即可运行,但是由于一般真机是不带有root权限的,即使SSH可以解决root权限,当到了java层,root权...

Android中间件开发----helloworld

1.主要是验证开发环境是否建好,先上一个运行结果图:

零配置 之 21.3 通过注解来实现零配置

通过注解来实现零配置 21.3.1  概述 在实际开发中,光是简单的通过默认的方式来进行配置,是满足不了实际项目的需要的,这就需要使用注解,来进一步控制程序各个部分的对应关系。 就其实质,就是...

add MEMBERS 添加项目

Understanding Instance and Class Members(Reprint from http://download.oracle.com/javase/tutorial/ja)

Understanding Instance and Class MembersIn this section, we discuss the use of the static keyword to...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)