Is your source code "pristine"? This was brought about by this codegolf question.

Problem

A program is said to be pristine if it is:


A working program (No errors).
The program will produce errors by removing any contiguous substring of N characters, where 1 &lt;= N &lt; program length. 


My goal is to check if a given C# program's source is pristine or not with the assumption that the given program is already working.

What I Did

I generated all possible removal combinations and compiled the resulting string with CompileAssemblyFromSource. Compile errors are then checked with CompilerResults.Errors.

static bool isPristine(string sourceCode)
{
    using (var csc = new CSharpCodeProvider(new Dictionary&lt;string, string&gt;() { { "CompilerVersion", "v3.5" } }))
    {
        var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" });
        parameters.GenerateExecutable = true;

        int length = sourceCode.Length;

        for (int i = 0; i &lt; length; i++)
        {
            for (int j = 1; j &lt; length - i; j++)
            {
                string sourceWithRemoval = sourceCode.Remove(i, j);
                CompilerResults results = csc.CompileAssemblyFromSource(parameters, sourceWithRemoval);

                if (results.Errors.Count == 0)
                {
                    Debug.WriteLine("Still works by removing:");
                    Debug.WriteLine("@{0}@ ", sourceCode.Substring(i, j));
                    Debug.WriteLine("At index {0}.", i);

                    return false;
                }
            }
        }
    }

    return true;
}


Usage

static void Main(string[] args)
{
    string sourceCode = "class e{static void Main(){System.Console.Write(new char[1\n#if l\n#else\n*5]{'w','o','r','l','d'\n#if o\n#else\n});\n#endif\n}\n#endif\n}";

    Console.WriteLine(isPristine(sourceCode) ? "Pristine" : "Not pristine");
    Console.ReadLine();
}


Main Concerns


What can I improve (performance, readability)?
Are there better, faster C# techs that I should have used?
A few minors really:


Standard C# naming convention for methods is PascalCase. So should be IsPristine.
If it's important (or nice) to know which specific sequence is yielding the false result then you should consider returning that from the method. Something like this:

public class PristineResult
{
    public readonly bool IsPristine;
    public readonly string OffendingSequence;
    public readonly string OffendingIndex;

    public PristineResult(bool isPristine, string offendingSequence = null, int offendingIndex = -1)
    {
        IsPristine = isPristine;
        OffendingSequence = offendingSequence;
        OffendingIndex = offendingIndex;
    }
}

static PristineResult IsPristine(string sourceCode)
{
    using (var csc = new CSharpCodeProvider(new Dictionary&lt;string, string&gt;() { { "CompilerVersion", "v3.5" } }))
    {
        var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" });
        parameters.GenerateExecutable = true;

        int length = sourceCode.Length;

        for (int i = 0; i &lt; length; i++)
        {
            for (int j = 1; j &lt; length - i; j++)
            {
                string sourceWithRemoval = sourceCode.Remove(i, j);
                CompilerResults results = csc.CompileAssemblyFromSource(parameters, sourceWithRemoval);

                if (results.Errors.Count == 0)
                {
                    return new PristineResult(false, sourceCode.Substring(i, j), i);
                }
            }
        }
    }

    return new PristineResult(true);
}