Handle "OutofMemoryException" in C#?
<p>In this code string <code>x</code> gives an <code>OutOfMemoryException</code>. Is there any other way that I can parse all the files without getting <code>OutofMemoryException</code>? There seems to be nothing wrong in the code I have tried.</p>

<p>Someone suggested to make the program read file by file rather than reading whole files and putting it in one string <code>x</code>.</p>

<pre><code>IEnumerable&lt;string&gt; textLines = Directory.GetFiles(@"C:\Users\karansha\Desktop\Unique_Express\", "*.*")
    .Select(filePath =&gt; File.ReadLines(filePath))
    .SelectMany(line =&gt; line);

string x = string.Join(",", textLines);
List&lt;string&gt; users = new List&lt;string&gt;();
Regex regex = new Regex(@"User:\s*(?&lt;username&gt;.*?)\s");
MatchCollection matches = regex.Matches(x);
foreach (Match match in matches)
{
    var user = match.Groups["username"].Value;
    if (!users.Contains(user)) users.Add(user);
}
int numberOfUsers = users.Count(name =&gt; name.Length &lt; 15); 
Console.WriteLine("Unique_Users_Express=" + numberOfUsers);
</code></pre>
