freeCodeCamp/guide/chinese/csharp/enumerations/index.md

48 lines
826 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Enumerations
localeTitle: 枚举
---
# 枚举
枚举是一组使用`enum`关键字声明的命名整数常量。
## 例
```
enum Gender
{
Male,
Female
}
```
默认情况下对于每个枚举名称整数值从0开始并增加1即Male = 0Female = 1等。
可以通过为任何枚举名称指定整数值来覆盖这些。
## 例
```
enum Gender
{
Male = 1,
Female
}
```
在这种情况下整数值将从1开始并从那里开始增加。
要使用枚举,您可以声明其类型的变量并为其赋值:
`Gender myVar = Gender.Male;`
您还可以将枚举名称值强制转换为基础整数值,反之亦然:
```
Console.WriteLine($"Male: {(int)Gender.Male}");
Console.WriteLine($"Female: {(int)Gender.Female}");
```
## 输出:
```
Male: 1
Female: 2
```