Concatenate List of enum to String with C#

So you have a list of enum values and you need to convert it to a single string separated by some character.

To demonstrate this we need an enumerated type:

public enum test{
    No,
    Yes,
    Maybe,
}

and a list of enum values:

var n = new List();
n.Add(test.Yes);
n.Add(test.Maybe);

To get the character delimited string we need to convert the list to an array, add a bit of Linq, and use string.Join to finally concatenate the values:

string.Join(" - ", n.Select(s => s.ToString()).ToArray())

The first parameter passed to the Join method is the separator between each list item.

Voila!

Enumeration Helper Extension Methods using Generics

Here is a helper class for enumerations. The purpose of the class is to use generics to wrap the current member methods, like Parse, but also to add additional methods, like returning the description of a Named Constant.

public static class EnumHelper
{
    public static Nullable<T> Parse<T>(String value, Boolean ignoreCase) where T : struct
    {
        return String.IsNullOrEmpty(value) ? null : (Nullable<T>)Enum.Parse(typeof(T), value, ignoreCase);
    }

    public static T[] GetValues<T>()
    {
        return (T[])Enum.GetValues(typeof(T));
    }

    public static string EnumToString<T>(object value)
    {
        return Enum.GetName(typeof(T), value);
    }

    public static T Parse<T>(String value, Boolean ignoreCase, T defaultEnum) where T : struct
    {
        if ((!string.IsNullOrEmpty(value)) && (Enum.IsDefined(typeof(T), value)))
            return (T)EnumHelper.Parse<T>(value, ignoreCase);
        else
            return defaultEnum;
    }

    public static string GetEnumDescription(Enum en)
    {
        Type type = en.GetType();
        MemberInfo[] memInfo = type.GetMember(en.ToString());
        if (memInfo != null && memInfo.Length > 0)
        {
            object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attrs != null && attrs.Length > 0)
                return ((DescriptionAttribute)attrs[0]).Description;
        }
        return en.ToString();
    }
}

To demonstrate how the helper methods are used I have written unit tests. The tests depend on the following enumeration to work.

public enum Days
{
    [Description("First day of week")]
    Monday,
    [Description("Second day of week")]
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday,
}

Here are the unit tests.

public class EnumHelperTests
{
    [SetUp]
    public void Setup()
    { }

    [TearDown]
    public void Teardown()
    { }

    [Test]
    public void EnumHelper_Parse_ReturnCorrectNamedConstant()
    {
        // Act
        var enumResult = EnumHelper.Parse<Days>("monday", true);

        // Assert
        Assert.AreEqual(enumResult, Days.Monday);
    }

    [Test]
    public void EnumHelper_EnumToString_ReturnsNamedConstantAsString()
    {
        // Act
        var day = EnumHelper.EnumToString<Days>(2);

        // Assert
        Assert.AreEqual(day, "Wednesday");
    }

    [Test]
    public void EnumHelper_GetEnumDescription_CorrectConstantDescription()
    {
        // Act
        var desc = EnumHelper.GetEnumDescription(Days.Monday);

        // Assert
        Assert.AreEqual("First day of week", desc);
    }

    [Test]
    public void EnumHelper_GetValues_ArrayOfNamedConstants()
    {
        //  Arrange
        var days = new Days[7] {
        Days.Monday,
        Days.Tuesday,
        Days.Wednesday,
        Days.Thursday,
        Days.Friday,
        Days.Saturday,
        Days.Sunday
    };

        // Act
        var enumDays = EnumHelper.GetValues<Days>();

        // Assert
        CollectionAssert.AreEqual(days, enumDays);
    }
}

A full demo app is available from Github here