Building a string representing 50+ ActiveDirectory options I have a switch statement used to handle options that a user selected (stored in an array). The array is iterated through, and for each element (enum object) in the array, an appropriate action is performed. However, the enum has over 50 items, so the switch statement is very long.

The items are all necessary and related, so they cannot be moved or removed. They also each call a method or get a property of an object, so no dictionaries. Are there any other options to make this shorter?

public string GetUserAttributes(UserPrincipal user,
    ActiveDirectoryUserAttribute[] attributes)
{
    var userAsDirectoryEntry
        = (DirectoryEntry)user.GetUnderlyingObject();
    var userAttributes = string.Empty;
    foreach (ActiveDirectoryUserAttribute attribute in attributes)
    {
        switch (attribute)
        {
            case ActiveDirectoryUserAttribute.AccountExpirationDate:
                userAttributes += AttributeToString(
                    user.AccountExpirationDate.ToString());
                break;
            case ActiveDirectoryUserAttribute.AccountLockoutTime:
                userAttributes += AttributeToString(
                    user.AccountLockoutTime.ToString());
                break;
            case ActiveDirectoryUserAttribute.Assistant:
                userAttributes += AttributeToString(
                    userAsDirectoryEntry.Properties[AttributeAssistant]
                    .Value.ToString());
                break;
            case ActiveDirectoryUserAttribute.BadLogonCount:
                userAttributes += AttributeToString(
                    user.BadLogonCount.ToString());
                break;
            ...
        }
    }
    return userAttributes;
}
You can put the switch-cases into a dictionary to reduce the boilerplate case and break. The foreach and the string concatenation part can also be reduced with string.Concat(...) or string.Join(delimiter, ...):

public string GetUserAttributes(UserPrincipal user, ActiveDirectoryUserAttribute[] attributes)
{
    var userAsDirectoryEntry = (DirectoryEntry)user.GetUnderlyingObject();

    var getters = new Dictionary&lt;ActiveDirectoryUserAttribute, Func&lt;object&gt;&gt;();
    getters[ActiveDirectoryUserAttribute.AccountExpirationDate] = () =&gt; user.AccountExpirationDate;
    getters[ActiveDirectoryUserAttribute.AccountLockoutTime] = () =&gt; user.AccountLockoutTime;
    getters[ActiveDirectoryUserAttribute.Assistant] = () =&gt; userAsDirectoryEntry.Properties[AttributeAssistant].Value;
    getters[ActiveDirectoryUserAttribute.BadLogonCount] = () =&gt; user.BadLogonCount;
    // ...

    // just concat them directly, like you are doing now
    return string.Concat(attributes.Select(attr =&gt; getters[attr]()));

    // or, you can separate them with a comma, or anything :
    return string.Join(", ", attributes
        .Select(attr =&gt; getters[attr]()));
}




You can also use the collection initializer for dictionary to cut down some letters :

var getters = new Dictionary&lt;ActiveDirectoryUserAttribute, Func&lt;object&gt;&gt;
{
    // C# 6
    [ActiveDirectoryUserAttribute.AccountExpirationDate] = () =&gt; user.AccountExpirationDate,
    [ActiveDirectoryUserAttribute.AccountLockoutTime] = () =&gt; user.AccountLockoutTime,

    // older version
    { ActiveDirectoryUserAttribute.Assistant, () =&gt; userAsDirectoryEntry.Properties[AttributeAssistant].Value },
    { ActiveDirectoryUserAttribute.BadLogonCount, () =&gt; user.BadLogonCount },

    // ...
};


Personally I prefer the C# 6 collection initializer the most, second by the method shown in first snippet, and lastly the older initializer.