C# used for comparing two lists

Title: C# used for comparing two lists
Alternative title: C# used in place of a scripting language

A friend of mine had two lists. Each list was in its own text file and each entry occupied one line. Each list had more that one thousand entries (lines). My friend wanted to find the entries (lines) that were in one of the two lists but not in the other. She was puzzled because this is difficult to do manually.

So, to make it more specific, my friend had List1.txt and List2.txt and wanted to find the entries (lines) that were in List2.txt but not in List1.txt.

She turned to me for help and, of course, I provided her with a List3.txt text file that contained the entries that were in List2.txt but not in List1.txt.
 
How did I solve this admittedly easy problem? I decided to create a simple C# console application that solved it.

Now, I could have chosen a number of ways to solve this problem. Scripting (a batch file, VBScript, PowerShell), any programming language and, also, ready made programs (applications for this and similar purposes).

My friend’s computer had the .NET Framework v2.0 installed, so I thought it would be nice to create a C# console application that would solve the problem in a way that was easy to understand and easy to modify in the future, should the need arise.

So, I used C# instead of using a scripting language. Now, the use of C# for such an easy task might be considered overkill, but it shouldn’t. Of course, a C# program needs compilation, but this is easily done. Here is how I did it:

First, I created the program in a text file named ListComp.cs.

using System;
using System.IO;

class CompareLists
{
    public static void Main()
    {

        string line;
        bool found;

        Console.WriteLine("Sequence begins.");

        System.Collections.Generic.List<string> myList1 = new System.Collections.Generic.List<string>();
        System.Collections.Generic.List<string> myList2 = new System.Collections.Generic.List<string>();

        //
        //Get data from List1.txt
        //
        try
        {
           using (StreamReader sr = new StreamReader("List1.txt"))
           {
              while ((line = sr.ReadLine()) != null)
              {
                 myList1.Add(line);
              }
           }
        }
        catch (Exception e)
        {
           Console.WriteLine(e.Message);
        }

        //
        //Get data from List2.txt
        //
        try
        {
           using (StreamReader sr = new StreamReader("List2.txt"))
           {
              while ((line = sr.ReadLine()) != null)
              {
                 myList2.Add(line);
              }
           }
        }
        catch (Exception e)
        {
           Console.WriteLine(e.Message);
        }

        //
        //Write in List3.txt the values that exist in List2.txt but not in List1.txt
        //
        try
        {
           using (StreamWriter outfile = new StreamWriter("List3.txt"))
           {
              foreach (string myEntry2 in myList2)
              {

                 found = false;

                 foreach (string myEntry1 in myList1)
                 {
                    if (myEntry1 == myEntry2)
                    {
                       found = true;
 
                       //No need to examine the rest of the List1 entries.
                       //Go to end of the foreach myList1.
                       break;      
                    }
                 } //foreach myList1

                 if (!found)
                 {
                    outfile.WriteLine(myEntry2);
                 }

              } //foreach myList2
           } //using StreamWriter
        }
        catch (Exception e)
        {
           Console.WriteLine(e.Message);
        }

        Console.WriteLine("Sequence ended.");
        Console.WriteLine("Press Enter to exit.");
        Console.ReadLine();

    }
}

Then I created a text file named Compilation.bat (the actual name does not matter), in order to compile ListComp.cs.

del ListComp.exe

%systemroot%\Microsoft.NET\Framework\v2.0.50727\csc ListComp.cs

pause

The batch file deletes ListComp.exe in case it already exists and then uses the C# compiler (csc.exe) to compile ListComp.cs, thus producing ListComp.exe.

I put ListComp.cs, Compilation.bat, List1.txt and List2.txt in a folder (the folder’s location is irrelevant). Then I clicked Compilation.bat to produce ListComp.exe and then I clicked ListComp.exe to produce List3.txt. Mission accomplished.

Please note that ListComp.exe overwrites List3.txt if it already exists in the folder, so be careful when you run it.

Advertisements

About Dimitrios Kalemis

I am a systems engineer specializing in Microsoft products and technologies. I am also an author. Please visit my blog to see the blog posts I have written, the books I have written and the applications I have created. I definitely recommend my blog posts under the category "Management", all my books and all my applications. I believe that you will find them interesting and useful. I am in the process of writing more blog posts and books, so please visit my blog from time to time to see what I come up with next. I am also active on other sites; links to those you can find in the "About me" page of my blog.
This entry was posted in Development. Bookmark the permalink.