枚举和字符串互转
C#
如果将一个枚举值转换为字符串就非常的简单,直接使用枚举的Tostring()就可以了。
那么将一个字符串转换成枚举可就有一点麻烦了。方法如下:
那么将一个字符串转换成枚举可就有一点麻烦了。方法如下:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string s = "ss";
            dd p = (dd)Enum.Parse(typeof(dd),s);
            MessageBox.Show(p.ToString());
        }
    }
    public enum dd
    {
        mm,ss
    }
// This code requires the use of the System namespace. 
enum Colors
{
red, green, blue
} 
string colorString = "blue";
// Note that the Parse method below is a method defined by System.Enum,
// not by Colors.
Colors actualEnum = (Colors)Colors.Parse(typeof(Colors), colorString);
// actualEnum = blue
C++:
 
                    
                     
                    
                 
                    
                 
                
            
         
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号