X hits on this document





43 / 396


the enum type and is a distinct valid value of that enum type.

The following example declares an enum type named Alignment with an underlying type of sbyte.

enum Alignment: sbyte { Left = -1, Center = 0, Right = 1 }

As shown by the previous example, an enum member declaration can include a constant expression that specifies the value of the member. The constant value for each enum member must be in the range of the underlying type of the enum. When an enum member declaration does not explicitly specify a value, the member is given the value zero (if it is the first member in the enum type) or the value of the textually preceding enum member plus one.

Enum values can be converted to integral values and vice versa using type casts. For example

int i = (int)Color.Blue;// int i = 2; Color c = (Color)2;// Color c = Color.Blue;

The default value of any enum type is the integral value zero converted to the enum type. In cases where variables are automatically initialized to a default value, this is the value given to variables of enum types. In order for the default value of an enum type to be easily available, the literal 0 implicitly converts to any enum type. Thus, the following is permitted.

Color c = 0;

1.11 Delegates

A delegate type represents references to methods with a particular parameter list and return type. Delegates make it possible to treat methods as entities that can be assigned to variables and passed as parameters. Delegates are similar to the concept of function pointers found in some other languages, but unlike function pointers, delegates are object-oriented and type-safe.

The following example declares and uses a delegate type named Function.

using System;

delegate double Function(double x);

class Multiplier { double factor;

public Multiplier(double factor) { this.factor = factor; }

public double Multiply(double x) { return x * factor; } }

class Test { static double Square(double x) { return x * x; }

30Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1202
Page views1202
Page last viewedWed Jan 18 22:53:27 UTC 2017