本文主要是介绍C# step by step 学习笔记8 CHAPTER 9 使用枚举和结构创建值类型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C# 2012 step by step 学习笔记8 CHAPTER 9 使用枚举和结构创建值类型
本章内容
- 声明一个枚举类型
- 创建并使用一个枚举类型
- 声明一个结构类型
- 创建并使用一个结构类型
- 解释结构和类之间行为的差别
声明一个枚举
enum Season { Spring, Summer, Fall, Winter }
使用枚举
You can assign a value that is defined by the enumeration only to an enumeration variable.
Note:As you can with all value types, you can create a nullable version of an enumeration variable by using the ? modifier. You can then assign the null value, as well the values defined by the enumeration, to the variable: Season? colorful = null;
All enumeration literal names are scoped by their enumeration type. This is useful because it allows different enumerations to coincidentally contain literals with the same name.
Many of the standard operators you can use on integer variables can also be used on enumeration variables (except the bitwise and shift operators, which are covered in Chapter 16, “Using Indexers”). For example, you can compare two enumeration variables of the same type for equality by using the equality operator (==), and you can even perform arithmetic on an enumeration variable (although the result might not always be meaningful!).
Choosing enumeration Literal Values
Internally, an enumeration type associates an integer value with each element of the enumeration. By default, the numbering starts at 0 for the first element and goes up in steps of 1.
这篇关于C# step by step 学习笔记8 CHAPTER 9 使用枚举和结构创建值类型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!