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'
- Program.cs: the file. A build of several files names each one, so the first job is to find the file you were editing.
- (2,9): the line and the column, counted from 1. The column points at the start of the expression the compiler stopped on, not at the semicolon.
- error: the severity. warning and info are the other two.
- CS0029: the code. It is stable across versions and is the fastest thing to search for.
- The message, which is a sentence about the rule that was broken.
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#:
- CS0029: a value of one type cannot be converted to another implicitly.
- CS0103: a name does not exist in the current context, which is a typo or a missing using.
- CS1061: a type has no such member.
- CS0161: not all code paths return a value.
- CS0246: a type or namespace was not found, which is a missing using or a missing package.
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
- Change total = "ten"; to total = int.Parse("ten"); and build. Which error do you get now, and why is it a different one?
- Add a third independent error to the second program. Does the compiler report all three?
- Rename Console to console in any program of this page. Which code does the compiler report, and at which column?
- 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.
-
What does the (2,9) in
Program.cs(2,9): error CS0029name?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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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