枚举类型本质上是人为限定了取值范围的几个整数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Level = Level.Employee;
Person boss = new Person();
boss.Level = Level.Boss;
Console.WriteLine(boss.Level > person.Level);
Console.WriteLine((int)Level.Employee);
Console.WriteLine((int)Level.Manager);
Console.WriteLine((int)Level.Boss);
Console.WriteLine((int)Level.BigBoss);
}
}
enum Level //默认值是0,1,2,3...(强制类型转换的话),但可以赋值
{
Employee=100,
Manager=200,
Boss=300,
BigBoss,
}
class Person
{
public int ID { get; set; }
public string Name { get; set; }
public Level Level { get; set; }
}
}
结果
比特位用法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "Tim";
person.Skill = Skill.Drive | Skill.Cook | Skill.Teach;
Console.WriteLine(person.Skill);
Console.WriteLine((person.Skill & Skill.Cook)==Skill.Cook);
}
}
enum Skill
{
Drive=1,
Cook=2,
Program=4,
Teach=8,
}
class Person
{
public int ID { get; set; }
public string Name { get; set; }
public Level Level { get; set; }
public Skill Skill { get; set; }
}
}
结果
结构体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
Student student = new Student() { ID = 101, Name = "梁" };
object obj = student;//装箱
Student student2 = (Student)obj;//拆箱
Console.WriteLine($"{student2.ID}Name:{student2.Name}");
}
}
struct Student
{
public int ID { get; set; }
public string Name { get; set; }
}
}
结语
课程到这里就结束了,我不是接触编程相关专业的学生,但通过学习c#,受益良多,很多之前没有明白的东西现在也通透了许多,如今编程已经用到了我的生活当中(虽然是以娱乐为主),在将来就业方面也不知道用不用得上,但现在我并不后悔学习c#,不说知识学到了多少,就解决问题的途径和方法,我也从刘铁猛老师的视频课程中收益良多,十分感谢老师。
课程内容已经结束,但学习从未停止,我也将写点例子巩固下c#知识。受疫情影响,我也不能拿到本专业的书籍(在学校),当下最轻松的学习就只有编程了。