[Previous] [Contents] [Next]

Compile-Time Errors


When a compiler, including the C# compiler, can't interpret what you're attempting to convey, it will print an error message and your application will fail to build. Type the following code into a file named HelloErrors.cs, and compile it: -

using Syste;

class HelloErrors
{
    public static void Main()
    {
        xConsole.WriteLine("Hello, World");
        Console.WriteLinex("Hello, World");
    }
}

Running the compiler will result in this output: -

HelloErrors.cs(1,7): error CS0234: The type or namespace name
' Syste' does not exist in the class or namespace ''

Keeping in mind that there's always a default, global namespace, this means that the compiler could not locate anything called Syste, for obvious reasons. However, what I want to illustrate here is what to expect when the compiler encounters syntax errors in your code. First you'll see the name of the current file being compiled, followed by the line number and column position of the error. Next you'll see the error code as defined by the compiler-in this case, CS0234.

Finally, after the error code, you'll see a short description of the error. Many times, this description will give you enough to make the error clear. However, if it doesn't, you can search for the error code in the .NET Framework SDK Documentation, which is installed with the .NET Framework SDK, for a more detailed description. The online help associated with the CS0234 error code is shown in Figure 3-3.

-

Figure 3-3 You can use the error code supplied by the C# compiler to find a description of the error in the online help documentation.-

Notice that although we introduced three errors-the System namespace is misspelled, the Console class is misspelled, and the call to the WriteLine method is misspelled-the compiler will report only one error. This is because certain errors, once they are encountered, cause the compiler to abort the compilation process and print the errors accrued up to that point. In this example, the compiler stopped its compilation process once it could not resolve the using directive because that error could be the cause of many more errors. Once you've correctly spelled the System namespace in the using directive, the compiler will report the line numbers and column positions of the remaining two errors.

[Previous] [Contents] [Next]