参考书:《 visual C# 从入门到精通》
第二部分 理解C#对象模型
第9章 使用枚举和结构创建值类型
文章目录
9.1 使用枚举
9.1.1 声明枚举
用关键字enum
。
enum Season{Spring,Summer,Fall,Winter};
9.1.2 使用枚举
Season colorful=Season.Fall;
Console.WriteLine(colorful);
9.1.3 选择枚举字面值
枚举内部的每个元素都关联一个整数值。默认第一个元素对应0
,后面每个元素对应的整数都递增1。
也可以手动将特定整数常量和枚举类型的字面值关联起来。
enum Season{Spring=1,Summer,Fall,Winter}
...;
Season clolorful=Season.Fall;
Console.WriteLine((int)colorful);
多个枚举字面值可具有相同的基础值。
enum Season{Spring,Summer,Fall=Autumn,Winter}
9.1.4 选择枚举的基础类型
枚举的字面值默认是int
类型,但可以让枚举类型基于不同的基础整型,可以是8种整型中的任何一种:byte
,sbyte
,short
,ushort
,int
,uint
,long
,ulong
。
如:enum Season:short{Spring,Summer,Fall,Winter}
如下面代码:
using System;
namespace C_8_1
{
enum Month
{
January,February,March,April,May,June,July,August,September,October,November,Decemnber
}
class Program
{
static void dowork()
{
Month first = Month.Decemnber;
Console.WriteLine(first);
first++;
Console.WriteLine(first);
}
static void Main(string[] args)
{
dowork();
}
}
}
9.2 使用结构
我们知道类定义的是引用类型,但有时我们只是需要处理少量的数据,这时需要管理堆而造成较多的开销。更好的做法是将类型定义成结构。结构是值类型,在栈上存储,这样能有效减少内存管理的 开销。
结构可包含自己的字段、方法和构造器。构造器与类的构造器是有区别的。
9.2.1 声明结构
用关键字struct
开头
struct Time{
public int hours,minutes,seconds;
}
和类一样,一般不要在结构中声明公共字段。可以使用私有字段,为结构添加构造器和方法来初始化和处理这些字段:
struct Time{
private int hours,minutes,seconds;
...;
public Time(in t hh,int ss){
this.hours=hh%24;
this.minus=mm%60;
this.seconds=ss%60;
}
public int Hours(){
return this.hours;
}
}
9.2.2 理解结构和类的区别
区别如下:
- 不能为结构声明默认(无参数)构造器,因为编译器始终会自动生成一个。且提供的非默认构造器必须初始化所有的字段,否则会编译错误。
- 类的实例字段可在声明时初始化,但结构不行。
struct Time{
private int hours=0;//错误
...;
}
9.2.3 声明结构变量
9.2.4 理解结构的初始化
using System;
namespace C_8_1
{
enum Month
{
January,February,March,April,May,June,July,August,September,October,November,Decemnber
}
struct Date
{
private int year;
private Month month;
private int day;
public Date(int ccyy, Month mm, int dd)
{
this.year = ccyy - 1900;
this.month = mm;
this.day = dd - 1;
}
public override string ToString()
{
string data = $"{this.month} {this.day + 1} {this.year + 1900}";
return data;
}
}
class Program
{
static void dowork()
{
Date defaultDate = new Date();
Console.WriteLine(defaultDate);
Date weddingAnniversary = new Date(2015, Month.July,4);
Console.WriteLine(weddingAnniversary);
}
static void Main(string[] args)
{
dowork();
}
}
}
9.2.5 复制结构变量
可将结构变量初始化为另一个结构变量,前提是哪个结构变量是已经初始化了的。
下面是一个例子,如果将其中的关键字struct
换成class
又会是另一种结果:
using System;
using System.Threading;
namespace C_8_1
{
enum Month
{
January,February,March,April,May,June,July,August,September,October,November,Decemnber
}
struct Date
{
private int year;
private Month month;
private int day;
public Date(int ccyy, Month mm, int dd)
{
this.year = ccyy - 1900;
this.month = mm;
this.day = dd - 1;
}
public override string ToString()
{
string data = $"{this.month} {this.day + 1} {this.year + 1900}";
return data;
}
public void AdvanceMonth()
{
this.month++;
if (this.month == Month.Decemnber + 1)
{
this.month = Month.January;
this.year++;
}
}
}
class Program
{
static void dowork()
{
Date weddingAnniversary = new Date(2015, Month.July,4);
Date copy = weddingAnniversary;
Console.WriteLine($"Value of copy is {copy}");
weddingAnniversary.AdvanceMonth();
Console.WriteLine($"New value of weddingAnniversary is {weddingAnniversary}");
Console.WriteLine($"Value of copy is still {copy}");
}
static void Main(string[] args)
{
dowork();
}
}
}