CS0029 implicit conversion: no cast will help
The compiler is not asking for a cast. It is telling you no route exists between these two types, and a different number covers the case where one does.
A CS0029 implicit conversion error means there is no route from the type you gave the compiler to the type it needs. Not a missing cast. No route.
Here is the whole thing from a real build:
Program.cs(2,12): error CS0029: Cannot implicitly convert type 'string' to 'int'
The two numbers are line and column. Column 12 lands on Console.ReadLine()
rather than on the variable, which is the first hint:
Console.Write("Your age: ");
int age = Console.ReadLine();
Console.ReadLine() returns a string. Always, even when someone types 20.
age is an int, so the compiler has text on one side and a whole number on the
other, and no rule in the language tells it what to do with twenty or with a
bare Enter. It stops instead of picking for you.
The fix names the operation:
int age = int.Parse(Console.ReadLine());
The catch is that int.Parse throws FormatException when the text isn’t a
number, so anything reading real user input wants int.TryParse:
if (int.TryParse(Console.ReadLine(), out int age))
{
Console.WriteLine($"Next year you'll be {age + 1}.");
}
else
{
Console.WriteLine("That's not a number.");
}
Typing twenty then prints That's not a number. instead of ending the program.
The compiler does convert on its own, but only where nothing can be lost. int
goes into long and into double unasked, because every int value fits both.
Even char goes into int:
char letter = 'K';
int code = letter;
That builds and prints 75, the character code for K. Nothing is lost there. The
other way round, int to char, needs a cast, because most int values are not
characters. Text and
numbers don’t fit the rule in either direction, and that is where CS0029 comes
from.
Two more variants: char to string and int to bool
The second one turns up the first time you reach for a character inside a string:
Program.cs(2,25): error CS0029: Cannot implicitly convert type 'char' to 'string'
word[0] gives you a char, a single character, not a string of length one. C#
doesn’t treat one as shorthand for the other, so you want word[0].ToString().
The third variant isn’t a type problem. It’s a typo:
Program.cs(4,5): error CS0029: Cannot implicitly convert type 'int' to 'bool'
One line is responsible:
if (age = 18)
One equals sign assigns, two compare. age = 18 evaluates to 18 and has type
int, and if wants a bool. In C and in JavaScript that line runs and quietly
does something you didn’t intend. Here the compiler stops, and it’s the same
refusal that looked obstructive when Console.ReadLine() ran into it.
Assignments aren’t the only place it fires. The same number comes out of a
return when the method declares int and gives back text:
Program.cs(5,12): error CS0029: Cannot implicitly convert type 'string' to 'int'
The column then points at the expression after return, not at the method
signature. Same message, different place.
CS0266 reads almost the same and wants something else
The CS0029 implicit conversion message and CS0266 differ in the sentence that follows the type names:
Program.cs(4,19): error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
Here a conversion exists and the compiler knows it. It won’t do it for you,
because (int) drops the decimal part, and losing data is something you have to
ask for. (int)average is the entire fix.
Whether the two types are related has nothing to do with it. object to string
is CS0266 as well, because the cast makes sense even though it can fail while the
program runs.
With CS0029 there is nothing to cast, so the same attempt produces a third error number:
Program.cs(1,12): error CS0030: Cannot convert type 'string' to 'int'
Which is why “add a cast”, the advice sitting under most C# type conversion questions, is a dead end here. Read past the type names first. That’s the whole check.
The message also follows your Windows display language, so on a Polish or German install the same error comes back translated. The English string is the one worth pasting into a search box.
An editor with a language server behind it flags both sides of the assignment before you run a build at all. DevJourney bundles Roslyn Language Server, the same engine Visual Studio uses. Reading a message like this is one of the things that actually decides whether you finish, and it has nothing to do with which language you picked.
The course outline shows the order these fundamentals fall into.