Creating a list of pairs after splitting the string containing all of the sets I am working on asp.net/MVC and I need to read a value from my web.config file. Splitting the list into multiple keys is not an option.

&lt;add key="DepartAssign" value="Depart1:PersonAssignTo1; Depart2:PersonAssignTo2"/&gt;


I handled the problem by doing the following (which I am sure that there is a better approach)

public List&lt;string&gt; departmentList = new List&lt;string&gt;();
public List&lt;string&gt; assignedtoList = new List&lt;string&gt;();
public string[] pair = ConfigurationManager.AppSettings["DepartAssign"].Split(';');
foreach (string s in pair)
{
     departmentList.Add(s.Split(':').First()); //add the department to the list
     assignedtoList.Add(s.Split(':').Last()); //add the assignedto to the list
 }


and of course, I would be able to match the department with the assigned person by using the same index. Not really the cleanest approach but it works for now.

any way to make this code better?
You could create your own custom section in your config file.

First define what each record looks like:

public class Department : ConfigurationSection
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("assignee", IsRequired = true)]
    public string Assignee
    {
        get { return (string)this["assignee"]; }
        set { this["assignee"] = value; }
    }
}


Define the a collection so the framework can work with your section

public class DepartmentCollection : ConfigurationElementCollection
{
    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }

    protected override string ElementName
    {
        get { return "Department"; }
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get { return new ConfigurationPropertyCollection(); }
    }

    public Department this[int index]
    {
        get { return (Department) BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            base.BaseAdd(index, value);
        }
    }

    public Department this[string name]
    {
        get { return (Department) BaseGet(name); }
    }

    public void Add(Department item)
    {
        base.BaseAdd(item);
    }

    public void Remove(Department item)
    {
        BaseRemove(item);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new Department();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return (element as Department).Name;
    }
}


Define what the list looks like

public class DepartmentList : ConfigurationSection
{
    private static readonly ConfigurationPropertyCollection DepartmentListProperties;
    private static readonly ConfigurationProperty DepartmentProperty;

    static DepartmentList()
    {
        DepartmentProperty = new ConfigurationProperty(
            "",
            typeof (DepartmentCollection),
            null,
            ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection
            );

        DepartmentListProperties = new ConfigurationPropertyCollection
                                       {
                                           DepartmentProperty
                                       };
    }

    public DepartmentCollection Departments
    {
        get { return (DepartmentCollection) base[DepartmentProperty]; }
        set { base[DepartmentProperty] = value; }
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get { return DepartmentListProperties; }
    }
}


Now, in your .config file specify the new type:

&lt;configSections&gt;
    &lt;section name="DepartmentAssigneeMapping" type="DatabaseCleanup.DepartmentList, DatabaseCleanup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/&gt;
&lt;/configSections&gt;


and add your section

&lt;DepartmentAssigneeMapping&gt;
    &lt;Department name="Depart1" assignee="PersonAssignTo1" /&gt;
    &lt;Department name="Depart2" assignee="PersonAssignTo2" /&gt;
&lt;/DepartmentAssigneeMapping&gt;


Now, in your class:

public List&lt;string&gt; departmentList;
public List&lt;string&gt; assignedtoList;

var mappings = (DepartmentList) ConfigurationManager.GetSection("DepartmentAssigneeMapping");

departmentList = (
    from Department department in mappings.Departments 
    select department.Name).ToList();
assignedtoList = (
    from Department department in mappings.Departments 
    select department.Assignee).ToList();


Your class is much nicer, and the config is way cleaner.

This is fully tested, and the results you are looking for are replicated.