惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
月光博客
月光博客
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
腾讯CDC
B
Blog RSS Feed
博客园 - Franky
爱范儿
爱范儿

博客园 - dragonpro

初创公司招聘 程序员 平面设计师 软件销售(成都) 分享一组红白机音乐Remix 初识北京 2008,再会,深圳 顾客想要的和说的不一样 CommunityServer系列之十一:优秀的URL重写机制 Community Server系列之十:让CS2支持中文搜索 Community Server系列之九:CS2中的用户管理1(MemberRole) Community Server系列之八:CS2中的CSContext Community Server系列之七:快速找到需要修改的文件[技巧] Community Server系列之六:CS2中的关键词及数据结构 Community Server系列之五:CS2中的Ajax原理 Community Server系列之四:Ajax在CS2.0中的应用1 Community Server系列之三:页面间关系2[介绍] Community Server系列之二:页面之间的关系1[介绍] Community Server系列之一:开篇简介 梦里的灵感 使用Enterprise Library DAAB架构灵活的数据提供层 异构数据库转换工具的结构说明
从String到enum的互换(string to enum to string)
dragonpro · 2006-07-02 · via 博客园 - dragonpro

Convert a string to an enumerated (enum) value.

Using the Enum.Parse method, you can easily convert a string value to an enumerated value.  Doing this requires the type of the enum and string value.  Adding the true argument will cause the case to be ignored.

Using the following enum for this example:

private enum Aircraft
{
   Beech,
   Cessna,
   Piper
}

You can easily convert the string to an enum value like this:

Aircraft air = (Aircraft) Enum.Parse(typeof(Aircraft), "Cessna", true);

Ideally you should wrap a try-catch around the Enum.Parse statement.


string s;
s = air .ToString();
s = Enum.GetName(typeof(Aircraft), air.Beech);

后者比前者性能高