前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >enum和int、string的转换操作

enum和int、string的转换操作

作者头像
全栈程序员站长
发布2022-07-15 15:58:11
3770
发布2022-07-15 15:58:11
举报

大家好,又见面了,我是全栈君

enum Countries { 中国 = 5, 美国, 俄罗斯, 英国, 法国 }

  • enum 和 int
    • enum -> int int num = (int)Countries.中国; //num=5 int[] nums = (int[])Enum.GetValues(typeof(Countries)); //nums={5,6,7,8,9}
    • int -> enum Countries country = (Countries)8; //country=Countries.英国
    • //http://hovertree.com/menu/csharp/
  • enum 和 string
    • enum -> string string str1 = Countries.俄罗斯.ToString(); //str1=”俄罗斯”; string str2 = Enum.GetName(typeof(Countries), 7); //str2=”俄罗斯”; string[] strArray = Enum.GetNames(typeof(Countries)); //strArray={“中国”,”美国”,”俄罗斯”,”英国”,”法国”};
    • string-> enum Countries myCountry = (Countries)Enum.Parse(typeof(Countries), “中国”); //myCountry=Countries.中国

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/120458.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档