c#之枚举,结构体

时间:2021-09-26 07:23:53

1.枚举

2.结构体

例子1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _038_结构体 {
//我们可以把结构体当成,几个类型组成了一个新的类型
//比如下面的这个就是使用了3个float类型的变量,来表示一个坐标类型
struct Position//position 表示结构体名
{
public float x;
public float y;
public float z;
} enum Direction
{
West,
North,
East,
South
} struct Path
{
public float distance;
public Direction dir;
}
class Program {
static void Main(string[] args)
{
//通过三个float类型的变量来表示一个敌人的坐标
//float enemy1X = 34;
//float enemy1Y = 1;
//float enemy1Z = 34; //float enemy2X = 34;
//float enemy2Y = 1;
//float enemy2Z = 34; //当使用结构体声明变量的时候,相当于使用结构体中所有的变量去声明
//Position enemy1Position;
//enemy1Position.x = 34;//可以通过.加上属性名来访问结构体中指定的变量
////使用结构体让程序变得更清晰
//Position enemy2Position; Path path1;
path1.dir = Direction.East;
path1.distance = ;
}
}
} 例子2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _012结构体
{
public struct Student
{ public string _name; //public 是访问修饰符,访问范围是公共的,一般条件满足别处都可以调用
public Sex _sex;
public int _age; }
public enum Sex
{
男,
女 }
class Program
{ static void Main(string[] args)
{
//大学管理系统 来一个就需要录入信息,信息有姓名,性别,年龄。。。。。。
//string zsName = "张三";
/ar zsSex = '男';
//int zsAge = 20; //string lsName = "李四";
/ar lsSex = '女';
//int lsAge = 25; //string wwName = "王五";
/ar wwSex = '女';
//int wwAge = 26;
//学校招了10000人,需要定义30000个变量
//public struct 结构名{ 字段,属性,方法 } Student zsPerson;
zsPerson._name = "张三";
zsPerson._sex = Sex.男;
zsPerson._age = ;
Student lsPerson;
lsPerson._name = "李四";
lsPerson._sex = Sex.女;
lsPerson._age = ;
Console.WriteLine(lsPerson._age); }
}
}
枚举:代码规范,把同类放在一起
public enum Sex
{
男,//默认代表0
女=,
middle
}
class Program
{ static void Main(string[] args)
{
//大学管理系统:现在招生,文法系招生性别记录男女,机电系性别记录爷们娘们
//数学系记录1,0 国贸记录boy girl
//学校下达一个规定,性别只能用男女,其他学院必须调用学校教务处的性别,不能随便自己定义,于是产生了枚举:规范代码,把同类的放在一起
//public enum 枚举名{ 值,值,值} #region 枚举转换Int
//Sex math = Sex.男;
//int num = (int)math;
//Console.WriteLine(num);
#endregion
#region int转枚举
//int num = 11;
//Sex s = (Sex)num;//int转换枚举后,如果有对应的枚举,就显示其内容,如果没有就显示源数字
//Console.WriteLine(s);
#endregion
#region String转枚举
//string str = "middle";
//Sex o= (Sex)Enum.Parse(typeof(Sex),str);
//Console.WriteLine(o);
#endregion
#region 把枚举转换为字符串
Sex s = Sex.middle;
string str = s.ToString();
Console.WriteLine(s);
#endregion
}
}
例子:
public enum Season
{ 春,
    夏,
   秋,

}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入季节");
string season = Console.ReadLine();
Season s;
switch (season)
{
case "春":
case "夏":
case "秋":
case "冬": s = (Season)Enum.Parse(typeof(Season), season); Console.WriteLine(s); break;
default: Console.WriteLine("您输入的不是季节"); break;
} }
}
例子:
using System; namespace _002_student结构
{
public struct Student
{
public long _nums;
public string _name;
public Sex _sex;
public int _grade;
}
public enum Sex
{
男,

}
class Program
{
static void Main(string[] args)
{
Student person1;
person1._nums = ;
person1._name = "huangwei";
person1._sex = Sex.男;
person1._grade = ; Console.WriteLine(person1._name);
Student person2;
person2._nums = ;
person2._name = "alin";
person2._sex = Sex.女;
person2._grade = ;
Console.WriteLine(person2._grade);
}
}
}
using System;

namespace _009_枚举
{
public struct Class
{
public int _num;
public string _name;
public Sex _sex;
public int _xuehao;
}
public enum Sex
{
男,

}
class Program
{
static void Main(string[] args)
{
Class person1;
person1._num = ;
person1._name="黄伟";
person1._sex = Sex.男;
person1._xuehao = ; Class person2;
person2._num = ;
person2._name = "小明";
person2._sex = Sex.女;
person2._xuehao = ; Console.WriteLine("姓名"+"\t"+"班级人数"+"\t"+"性别"+"\t"+"学号");
Console.WriteLine(person1._name+"\t"+person1._num+"\t"+" "+person1._sex+"\t"+person1._xuehao);
}
}
}