C# > Types > Enum > String Enum
Implement Strings Enum in C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EnumString
{
public partial class Form1 : Form
{
public enum Cars
{
[Description("Audi")] Audi,
[Description("Bmw")] Bmw,
[Description("Land Rover")] LandRover
}
public static string GetCarDescription(Enum car)
{
FieldInfo fi = car.GetType().GetField(car.ToString());
DescriptionAttribute[] da = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (da != null && da.Length > 0)
return da[0].Description;
else
return da.ToString();
}
public static IEnumerable EnumToList()
{
Type type = typeof(T);
Array vals = Enum.GetValues(type);
List valList = new List(vals.Length);
foreach (int val in vals)
{
valList.Add((T)Enum.Parse(type, val.ToString()));
}
return valList;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Cars car in EnumToList<Cars>())
{
comboBox1.Items.Add(GetCarDescription(car));
}
}
}
}