c#如何获取枚举的值中的关联值有什么用

您所在的位置: &
C# 枚举常用方法浅析
C# 枚举常用方法浅析
C# 枚举常用方法是什么呢?在C# 枚举的学习中,C# 枚举常用方法十分的重要,那么C# 枚举常用方法的各自使用特点是什么呢?本文就向你介绍这方面的内容。
C# 枚举常用方法对于C# 枚举的学习十分重要,那么我们现在来看看什么是C# 枚举常用方法:
C# 枚举常用方法&1&获取枚举字符串TimeOfDay&time&=&TimeOfDay.A &&Console.WriteLine(time.ToString());&
C# 枚举常用方法&2&Enum.Parse()方法
这个方法带3个参数,第一个参数是要使用的枚举类型。其语法是关键字typeof后跟放在括号中的枚举类名。typeof运算符将在第5章详细论述。第二个参数是要转换的字符串,第三个参数是一个bool,指定在进行转换时是否忽略大小写。最后,注意Enum.Parse()方法实际上返回一个对象引用―― 我们需要把这个字符串显式转换为需要的枚举类型(这是一个取消装箱操作的例子)。对于上面的代码,将返回1,作为一个对象,对应于TimeOfDay.Afternoon的枚举值。在显式转换为int时,会再次生成1。TimeOfDay&time2&=&(TimeOfDay)& &&Enum.Parse(typeof(TimeOfDay),&"afternoon",&true); &&Console.WriteLine((int)time2);&
C# 枚举常用方法&3&得到枚举的某一值对应的名称lbOne.Text&=&Enum.GetName(typeof(TimeOfDay),&0); &&lbOne.Text&=&((TimeOfDay)0).ToString();&
两种方法都能实现,但是当其值越界(不是枚举所列出的值),就有一定的区别了。大家可以根据自己的需求不同,选择合适的方法。lbCon.Text&=&((TimeOfDay)5).ToString();& &&&&this.lbGetName.Text&=&Enum.GetName(typeof(TimeOfDay),&5);& &&&
C# 枚举常用方法&4&得到枚举的所有的值foreach&(int&i&in&Enum.GetValues(typeof(TimeOfDay))) &lbValues.Text&+=&i.ToString();&
C# 枚举常用方法&5&枚举所有的名称foreach(string&temp&in&Enum.GetNames(typeof(TimeOfDay))) &lbNames.Text+=&
C# 枚举常用方法的基本情况就向你介绍到这里,希望对你了解和学习C# 枚举有所帮助。
【编辑推荐】
【责任编辑: TEL:(010)】
关于的更多文章
AngularJS是很多Web开发人员在打造单页面应用程序时的首选创建方
随着云计算、物联网、大数据、移动互联网的大发展,你应该知道这些。
讲师: 35人学习过讲师: 17人学习过讲师: 253人学习过
我们都知道在20世纪90年代是多元文化和多媒体时代,而
Java 8版本最大的改进就是Lambda表达式,其目的是使Ja
美国旧金山时间3月31日,在红木城Oracle公司总部,Ora
本书虽然是《网管员必读―网络应用》的改版,但它绝不是简单的修改,而是完完全全的重写,内容更实用、更专业。全书共9章,13个
51CTO旗下网站------解决方案--------------------Color&my&=&(Color)(-1);
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有enum C#中枚举类型和radiobox关联操作代码 - 为程序员服务
为程序员服务
C#中枚举类型和radiobox关联操作代码
假如一个星期的enum:
星期一 = 0;,
关联到7个RadioButton,也就是单选框。
第一步在enum中定义星期一=0;
第二步在初始化函数中如下定义:
public MainForm()
// The InitializeComponent() call is required for Windows Forms designer support.
InitializeComponent();
// TODO: Add constructor code after the InitializeComponent() call.
int idx = 0;
foreach(Control c in groupBox1.Controls)
if(c is RadioButton)
((RadioButton)c).Text
= ((星期)idx).ToString();
((RadioButton)c).Tag = ((星期)idx);
第三步添加测试代码:
void Button1Click(object sender, EventArgs e)
foreach(Control c in groupBox1.Controls)
if(c is RadioButton)
if(((RadioButton)c).Checked == true)
星期 week = (星期)(((RadioButton)c).Tag);
MessageBox.Show(week.ToString());
注意:groupbox中控件的顺序在这些代码中控制,假如发现顺序不对,就要重新调整一下。
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton3);
this.groupBox1.Controls.Add(this.radioButton4);
this.groupBox1.Controls.Add(this.radioButton5);
this.groupBox1.Controls.Add(this.radioButton6);
this.groupBox1.Controls.Add(this.radioButton7);
您可能的代码
相关聚客文章
相关专栏文章C#中枚举 中的数字有什么用_百度知道
C#中枚举 中的数字有什么用
BigInt&这个字符串代表枚举值,需要6个字节,用int只要4个字节,枚举的本质就是给不同数字一个有意义阅读和记忆的名字。事实上其实枚举真实在计算机中的运算和存储就是用后面的数字表示。你想,如果用&quot
为您推荐:
其他类似问题
您可能关注的内容
等待您来回答(window.slotbydup=window.slotbydup || []).push({
id: '3284507',
container: s,
size: '0,0',
display: 'inlay-fix'
c#多选枚举怎么判断
C# 中枚举类型是一种值类型,目前(vs2012)还不能用于泛型。
此类型最多的用处是标识一组相同类型的状态量或常量,比如:
public enum ConnectionState
Closed = 0,
Connecting = 2,
Executing = 4,
Fetching = 8,
Broken = 16,
[ComVisible(true)]
[Serializable]
public enum FileAttributes
ReadOnly = 1,
Hidden = 2,
System = 4,
Directory = 16,
Archive = 32,
Device = 64,
Normal = 128,
Temporary = 256,
SparseFile = 512,
ReparsePoint = 1024,
Compressed = 2048,
Offline = 4096,
NotContentIndexed = 8192,
Encrypted = 16384,
[ComVisible(false)] IntegrityStream = 32768,
[ComVisible(false)] NoScrubData = 131072,
在使用中,平时使用频率最多的是:使用switch case 语句来判断枚举中的一个值,比如:
enum ActionStep
/// &summary&
/// 第一步:打开主页
/// &/summary&
/// &summary&
/// 第二步:查找输入框与提交按钮,输入关键字后提交
/// &/summary&
FindAndQuery,
/// &summary&
/// 第三步:查找指定连接,找到后导航到它
/// &/summary&
/// &summary&
/// 第四步:等待15秒后关闭
/// &/summary&
WaittingForClose,
switch (action.Step)
case ActionStep.Home:
Navigate(task.Home);
case ActionStep.FindAndQuery:
InputAndSubmit(task);
case ActionStep.Search:
SearchTarget(task);
case ActionStep.WaittingForClose:
WaitForClose(task);
然而,我们有时候也需要多状态的情况,比如:文件属性 FileAttributes。
那么,如何来操作呢?
正常的思路是声明一个变量,在需要的时候进行 与 或 非 操作。(不是还有hasFlag嘛?......),比如:C# Enum设计和使用的相关技巧 中的示例
例如定义了权限Enum Permission:
public enum Permission{
Select = 1,
Delete = 4,
All = Select | Edit | Delete | View
可以采用这个函数进行计算:
public static Permission ClearFlag(Permission value, Permission flag)
value = value & (Permission.All^ flag);
使用起来应该很方便,但是如果遇到,像下面的情况
public enum AuthorState
BlogListDownloading = 1,
BlogListParsing = 2,
BlogDownloading = 4,
BlogParsing = 8,
BlogWritting = 16,
MetaDownloading = 32,
MetaParsing = 64,
MetaWritting = 128,
MetasParsedDone = 2048,//所的的元素解析全部完成
DownloadList = 256,
CheckUpdate = 512,
NeedUpdate = 1024//需要更新
/// &summary&
/// 状态辅助类
/// &/summary&
public class EnumHelper
private AuthorState _
/// &summary&
/// 添加一个状态
/// &/summary&
/// &param name=&state&&&/param&
public void In(AuthorState state)
if (!Include(state))
/// &summary&
/// 删除一个状态
/// &/summary&
/// &param name=&state&&&/param&
public void Out(AuthorState state)
if(!Include(state))
//_state ^=
/// &summary&
/// 判断一个状态是否在其中
/// &/summary&
/// &param name=&state&&&/param&
/// &returns&&/returns&
public bool Include(AuthorState state)
return (_state & state) ==
&span style=&color: #993366;&&&strong& State.In(AuthorState.DownloadList);&/strong&&/span&
_download.DownloadBlogList(url);
&strong&&span style=&color: #993366;&&State.Out(AuthorState.DownloadList);
&/span&&/strong&
即,添加了一个状态,在之后的操作中会去除这个状态。
然而,我在写出了示例五之后总感觉这样做代码量增加太多了,虽然有智能提示。很是怀想delphi下的 In 操作:
procedure TForm1.Edit1KeyPress(Sender: TO var..Key: Char);
  if not(key in['0'..'9',#8])then
    key:=#0;
    MessageBeep(1); //B 调用系统声音也行!
C#中的In是不会让你这么用的,那么,要怎么办?扩展!扩展枚举,增加一些方法
google了一下,找到了 如何:为枚举创建新方法(C# 编程指南)
下面是我修改之后的代码
public enum MyEnum
Zero =1 , One = 2, Two = 4, Three =8
public static class Extensions
public static MyEnum Host = MyEnum.O
public static bool Contained(this MyEnum value)
return (Host & value) ==
public static void Add(this MyEnum value)
if (value.Contained())
public static void Remove(this MyEnum value)
if (value.Contained())
public class TestCls
private MyEnum _initValue = MyEnum.O
public void Test(MyEnum value)
Extensions.Host = _initV//控制变量
value.Contained();
value.Add();
value.Remove();
那么,好吧,你现在会用了。
MyEnum.One.Add();
MyEnum.One.Remove();
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------

我要回帖

更多关于 如何获取枚举的值 的文章

 

随机推荐