How to optimize this C# code
If you want to optimize for speed and possibly memory, just use regular loops to build up your dictionary, though it may hurt readability.

The idea is simple, scan the string for keys and values simultaneously.  Don't extract substrings until you know what you need.  Scan each token until you hit a delimiter and add the corresponding keys and values to your dictionary.

Assuming none of your keys or values do not contain any of your delimiters and everything is well formed and you have the same amount of values as you do keys for every string.

var myStrings = new[]
{
    "1-2-3-4-5|a-b-c-d-e",
    "10-20-30-40-50|aaa-bbb-ccc-ddd-eee",
};
var delims = new[] { '-', '|' };
var dict = new Dictionary&lt;string, string&gt;();
foreach (var str in myStrings)
{
    var sepIndex = str.IndexOf('|');

    var keyStart = 0;
    var valueStart = sepIndex + 1;
    while (keyStart &lt; sepIndex)
    {
        // scan for the end of the token
        var keyEnd = str.IndexOfAny(delims, keyStart);
        var valueEnd = keyEnd &lt; sepIndex
            ? str.IndexOfAny(delims, valueStart)
            : str.Length;

        // extract and add the tokens
        var key = str.Substring(keyStart, keyEnd - keyStart);
        var value = str.Substring(valueStart, valueEnd - valueStart);
        dict.Add(key, value);

        keyStart = keyEnd + 1;
        valueStart = valueEnd + 1;
    }
}




For readability, a declarative style works well and LINQ is king, just use it better.

var dict =
    (from str in myStrings
    let split = str.Split('|')
    let keys = split[0].Split('-')
    let values = split[1].Split('-')
    from pair in keys.Zip(values, (key, value) =&gt; new { key, value })
    select pair).ToDictionary(x =&gt; x.key, x =&gt; x.value);


As a side note, don't throw things into a list just for the sake of using the ForEach() method, you don't need the overhead of the list just to be able to interate over a collection through that method.