Replacing all keys found in a dictionary for its value in a list I have a file that is written DOS format and I need to replace some characters into others. I created a dictionary for that purposes, and now I have to read everyline from my stream and replace my values as I go.

I created this piece of code in C#, however, I highly doubt that this is the way to go, since I figure that C# dictionary class must have a simpler way to this probably common operation. Any feedback is greatly appreciated !

public Dictionary&lt;char, string&gt; Chars { get; set; } = new Dictionary&lt;char, string&gt;();

    // Called in my constructor
    public void CreateDictionnary()
    {
        Chars.Add('\u001b', " ");
        Chars.Add('\u0000', " ");
        // Multiple other characters that I have to replace
    }

public List&lt;string&gt; ReplaceSpecialCharacters(List&lt;string&gt; lines)
    {
        var result = new List&lt;string&gt;();

        foreach (var line in lines)
        {
            string newLine = string.Empty;

            foreach (var @char in line)
            {
                string replacedChar = @char.ToString();
                bool keyFound = false;

                foreach (var entry in Chars)
                {
                    if (keyFound)
                        continue;

                    if (@char == entry.Key)
                    {
                        replacedChar = entry.Value;
                        keyFound = true;
                    }
                }

                newLine += replacedChar;
            }

            result.Add(newLine);
        }

        return result;
    }
The main suggestion I'd make is to use Dictionary&lt;char,char&gt;.  This allows to work with char collections and convert them to strings rather than continuously concatenating strings.  

This also allows you to leverage LINQ to do the replacement and shorten your code tremendously. 

It could look like this:

public Dictionary&lt;char, char&gt; Chars { get; set; } = new Dictionary&lt;char, char&gt;();

// Called in my constructor
public void CreateDictionnary()
{
    Chars.Add('\u001b', ' ');
    Chars.Add('\u0000', ' ');
    // Multiple other characters that I have to replace
}
public List&lt;string&gt; ReplaceSpecialCharacters(List&lt;string&gt; lines)
{
    for(int i = 0; i &lt; lines.Count; ++i)
    {
        lines[i] = new string(lines[i].Select(x =&gt; Chars.ContainsKey(x) ? Chars[x] : x).ToArray());
    }
    return lines;
}


Incidentally this lowers the complexity from O(n²m),the total number of characters squared times the number of entries in the dictionary, to O(n²).  Which since you're dealing with strings is probably about the best you can get.  If you want it better, storing the data as char arrays instead of strings allows the replacement to be in-place instead of creating new strings.  This could be done in O(n) complexity.