[ Pobierz całość w formacie PDF ]

declare an underlying type of byte, sbyte, short, ushort, int, uint, long or ulong. Note that char
cannot be used as an underlying type. An enum declaration that does not explicitly declare an underlying type
has an underlying type of int.
The example
enum Color: long
{
Red,
Green,
Blue
}
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 233
C# LANGUAGE REFERENCE
declares an enum with an underlying type of long. A developer might choose to use an underlying type of
long, as in the example, to enable the use of values that are in the range of long but not in the range of int, or
to preserve this option for the future.
14.2 Enum members
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. An enum declaration can not contain
declarations of methods, properties, events, operators, or types.
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. The example
enum Color: uint
{
Red = -1
Green = -2,
Blue = -3
}
is in error because the constant values -1, -2, and  3 are not in the range of the underlying integral type uint.
Multiple enum members may share the same associated value. 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.
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
previous enum member by one. This increased value must be within the range of values that can be
represented by the underlying type.
The example
using System;
234 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 14 Enums
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
Blue = 10
Green = 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 Blue is explicitly given the value 10;
" and the enum member Green is automatically assigned the value one greater than the member that textually
precedes it.
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.
The example
enum Circular
{
A = B
B
}
is invalid because the declarations of A and B are circular. A depends on B explicitly, and B depends on A
implicitly.
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 235
C# LANGUAGE REFERENCE
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.
14.3 Enum values and operations
Each enum type defines a distinct type; an explicit enumeration conversion (§6.2.2) is required to convert
between an enum type and an integral type, or between two enum types. The set of values that an enum type can
take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be
cast to the enum type, and is a distinct valid value of that enum type.
Enum members have the type of their containing enum type (except within other enum member initializers: see
§14.2). The value of an enum member declared in enum type E with associated value v is (E)v.
The following operators can be used on values of enum types: ==, !=, , = (§7.9.5), + (§7.7.4),
- (§7.7.5), ^, &, | (§7.10.2), ~ (§7.6.4), ++, -- (§7.5.9, §7.6.7), sizeof (§7.5.12).
Every enum type automatically derives from the class System.Enum. Thus, inherited methods and properties of
this class can be used on values of an enum type.
236 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 15 Delegates
15. Delegates
15.1 Delegate declarations
delegate-declaration:
attributesopt delegate-modifiersopt delegate result-type identifier ( formal-parameter-listopt
) ;
15.1.1 Delegate modifiers
delegate-modifiers:
delegate-modifier
delegate-modifiers delegate-modifier
delegate-modifier:
new
public
protected
internal
private
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 237
Chapter 16 Exceptions
16. Exceptions
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 239
Chapter 17 Attributes
17. Attributes
Much of the C# language enables the programmer to specify declarative information about the entities defined
in the program. For example, the accessibility of a method in a class is specified by decorating it with the [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • apsys.pev.pl