Read the first compiler error

The shape of a diagnostic

Every line the compiler prints about a problem has the same five parts, in the same order:

Program.cs(2,9): error CS0029: Cannot implicitly convert type 'string' to 'int'

The rest of the output is bookkeeping: Build FAILED., the same diagnostics again under it, and the counts. Read the first diagnostic, fix it, and build again: a later error is often a consequence of the first one.

One error, read line by line

This program assigns a string to an int:

int total = 0; total = "ten"; Console.WriteLine(total);

dotnet build

Program.cs(2,9): error CS0029: Cannot implicitly convert type 'string' to 'int'

Build FAILED.

Program.cs(2,9): error CS0029: Cannot implicitly convert type 'string' to 'int'
0 Warning(s)
1 Error(s)

Time Elapsed 00:00:06.99

Line 2, column 9 is the total on the left of the assignment: that is the variable whose type does not fit, and the compiler names the conversion it cannot do. It does not say "you meant int.Parse" because the rule is about types, not about intent; the lesson on parsing text into values shows the conversion that does work.

No assembly was written, the previous one is still in bin from the last successful build, and $? is 1. A build that fails never replaces the output of a build that succeeded, which is why a stale program can run after a failed build.

Two errors in one file

The compiler does not stop at the first error; it reports what it can and then gives up. This file has two independent mistakes:

int count = "two"; int total = count + missing; Console.WriteLine(total);

dotnet build

Program.cs(1,13): error CS0029: Cannot implicitly convert type 'string' to 'int'
Program.cs(2,21): error CS0103: The name 'missing' does not exist in the current context

Build FAILED.

Program.cs(1,13): error CS0029: Cannot implicitly convert type 'string' to 'int'
Program.cs(2,21): error CS0103: The name 'missing' does not exist in the current context
0 Warning(s)
2 Error(s)

Time Elapsed 00:00:06.26

The two are independent: fixing the first does not fix the second, and the build still fails with one error after the first fix. A programmer reads them in order and builds again, because an error early in the file can hide the ones after it and a later error is often a consequence of an earlier one.

The error code

The code is the part worth remembering, because it is what a search box wants and what the documentation is indexed by. Five codes cover most of the first week of C#:

A warning next to an error

Warnings and errors share the output, and the warning may come first. This file has an unused variable and a misspelled member:

int unused = 42; string name = "Ada"; Console.WriteLine($"{name.ToUpper()} and {name.Lengths}");

dotnet build

Program.cs(3,48): error CS1061: 'string' does not contain a definition for 'Lengths' and no accessible extension method 'Lengths' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Program.cs(1,5): warning CS0219: The variable 'unused' is assigned but its value is never used

Build FAILED.

Program.cs(1,5): warning CS0219: The variable 'unused' is assigned but its value is never used
Program.cs(3,48): error CS1061: 'string' does not contain a definition for 'Lengths' and no accessible extension method 'Lengths' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
1 Warning(s)
1 Error(s)

Time Elapsed 00:00:06.33

The summary below Build FAILED. sorts warnings before errors, so the warning line comes first there although the compiler reported the error first. The severity word is what matters, not the position: CS0219 did not stop the build, and CS1061 did. The lesson on warnings as errors makes the warning stop it too.

Exercises

  1. Change total = "ten"; to total = int.Parse("ten"); and build. Which error do you get now, and why is it a different one?
  2. Add a third independent error to the second program. Does the compiler report all three?
  3. Rename Console to console in any program of this page. Which code does the compiler report, and at which column?
  4. Write a method that returns int and has a path with no return. Read CS0161.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the (2,9) in Program.cs(2,9): error CS0029 name?

    Answer

    The line and the column of the expression the compiler stopped on

    The pair is a line and a column counted from 1, which points at the start of the expression in the source.

  2. Which part of a diagnostic is the one to search the documentation for?

    Answer

    The code, such as CS0029

    The code is stable across versions and is what the documentation is indexed by; the position changes with the edit.

  3. What does this build report? int total = 0; total = "ten"; Console.WriteLine(total);

    Answer

    error CS0029: Cannot implicitly convert type 'string' to 'int'

    A string cannot be stored in an int, so the compiler reports CS0029 on the assignment.

  4. What does this build report? Console.WriteLine(Count);

    Answer

    error CS0103: The name 'Count' does not exist in the current context

    The name Count was never declared, so the compiler cannot resolve it and reports CS0103.

  5. What is the exit status of a failed dotnet build?

    Answer

    1

    A failed build leaves 1 in $?, which is what a script tests; 134 belongs to an unhandled exception at run time.

  6. What does this build report? string name = "Ada"; Console.WriteLine(name.Lengths);

    Answer

    error CS1061: 'string' does not contain a definition for 'Lengths'

    Lengths is not a member of string, so the compiler reports the missing member with CS1061.

  7. Why does the summary below Build FAILED repeat the diagnostics?

    Answer

    MSBuild lists the warnings and errors of the build again, warnings first, with the counts

    The console logger prints each diagnostic as it arrives and repeats the list in the summary, sorted by severity.

  8. After a failed build, which program is in bin?

    Answer

    The output of the last successful build, if there was one

    A failed compile writes no assembly, so the previous one stays, which is why a stale program can still run.

  9. What does this build report? Console.WriteLine("start"); Console.WriteLine(1 / 0);

    Answer

    error CS0020: Division by constant zero

    A division of two constants is computed by the compiler, and CS0020 reports the constant zero divisor.

  10. Which pair of numbers tells you where an error is in a file with a thousand lines?

    Answer

    The line and the column after the file name

    The position in parentheses is the only place in the line that locates the expression, and the file name precedes it.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev