网站开发与维护实训总结,wordpress图片上传后显示不出来,网站优化防范,wordpress站点错误上一篇#xff1a;
C##xff0c;入门教程(10)——常量、变量与命名规则的基础知识https://blog.csdn.net/beijinghorn/article/details/123913570 不会枚举#xff0c;就不会编程#xff01;
枚举 一个有组织的常量系列
比如#xff1a;一个星期每一天的名字#xf…上一篇
C#入门教程(10)——常量、变量与命名规则的基础知识https://blog.csdn.net/beijinghorn/article/details/123913570 不会枚举就不会编程
枚举 一个有组织的常量系列
比如一个星期每一天的名字周一、周二。。。周日。
所有成系列的常量都应该用枚举方式加以定义与使用。 一、枚举的定义
C#用 enum 定义枚举。
每个项都被自动赋予了一个(整数类型)值。对于整数类型项值是递增的。默认是从 0 开始的也就是项 1 的值是 0、项 2 的值是 1。如果不需要系统自动为项指定值也可以直接为其赋一个数值。每个没有指定值的项它的初始值都是上一个项的值 1。可以任意指定某项为特定的值。拗口吧看文字学不到的多看看代码就能理解了。
1、基本格式
// 完整方式
访问修饰符 enum 枚举名字 : 数据类型可无
{枚举项 初值可无,
}// 实例
public enum GradeName : int
{幼儿园 0,小学,初中,高中,大学 10,硕士,博士
}2、简约模式 // 简约模式默认数据类型int
// 第一项默认从0开始
public enum GradeName
{Undefined -1,幼儿园,小学,初中,高中,大学,硕士,博士
} 二、枚举的使用
定义好的枚举类似于一种数据类型用起来与 int 、double 差不多。
// 类
public class StudentInfo
{public GradeTitle Grade { get; set; } GradeTitle.Undefined;
}// 幼儿园毕业毕业证呢
// 拿不出来那你没有最低学历
if(firstStudent.Grade GradeTitle.幼儿园)
{;
}又所有枚举的第一个项一定是 Undefined !!!
枚举很简单用好不简单。 三、枚举与字符串的对照方法 有些情况下为了代码或结果的可阅读性期望将 枚举 与 字符串对照起来。
代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;namespace DataBeginner
{public partial class Form1 : Form{public Form1(){InitializeComponent();this.StartPosition FormStartPosition.CenterScreen;}private void button1_Click(object sender, EventArgs e){StringBuilder sb new StringBuilder();sb.AppendLine(2 is EnumDescription(EnglishNumbers.Two));webBrowser1.DocumentText sb.ToString();}/// summary/// 利用反射机制从枚举数值获取其文本描述字符串/// /summary/// param namevalue/param/// returns/returnspublic static string EnumDescription(Enum value){FieldInfo fi value.GetType().GetField(value.ToString());DescriptionAttribute[] attributes (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);return (attributes.Length 0) ? attributes[0].Description : value.ToString();}}public enum EnglishNumbers{[Description(One)] One 1,[Description(Two)] Two 2,[Description(Three)] Three 3,[Description(Four)] Four 4,[Description(Five)] Five}
}下一篇
C#入门教程(12)——数组及数组使用的基础知识https://blog.csdn.net/beijinghorn/article/details/123918227 文章转载自: http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.tpnch.cn.gov.cn.tpnch.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.hxpsp.cn.gov.cn.hxpsp.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.fmkbk.cn.gov.cn.fmkbk.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.rxhsm.cn.gov.cn.rxhsm.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.qgjwx.cn.gov.cn.qgjwx.cn http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.zgdnz.cn.gov.cn.zgdnz.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.sqgsx.cn.gov.cn.sqgsx.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn