8.11 Enums

8.11 Enums
An enum type declaration defines a type name for a related group of symbolic
constants. Enums are used for
.multiple choice. scenarios, in which a runtime decision is made from a
fixed number of choices that are
known at compile-time.
The example
enum Color
{
Red,
Blue,
Green
}
class Shape
{
public void Fill(Color color) {
switch(color) {
case Color.Red:
.
break;
case Color.Blue:
.
break;
Chapter 8 Language Overview
45
case Color.Green:
.
break;
default:
break;
}
}
}
shows a Color enum and a method that uses this enum. The signature of the
Fill method makes it clear
that the shape can be filled with one of the given colors.
The use of enums is superior to the use of integer constants.as is common
in languages without enums.
because the use of enums makes the code more readable and self-documenting.
The self-documenting nature
of the code also makes it possible for the development tool to assist with
code writing and other .designer.
activities. For example, the use of Color rather than int for a parameter
type enables smart code editors to
suggest Color values.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值