error CS1503: Argument 1: cannot convert from 'string' to 'int' means you passed
text to a method that expects a number. Text that looks like a number is still text in C#.
Convert it first, with int.TryParse when the text comes from a user, or
int.Parse when it is guaranteed to be valid.
"Argument 1" is the position of the bad argument in the call, counting from one, so the message tells you which value to convert. In a call with several arguments, fix only the one it names; the others already match their parameters. The quoted types say what you passed first and what the method wanted second.
The error
...\Program.cs(3,16): error CS1503: Argument 1: cannot convert from 'string' to 'int' [...\FixLab.csproj]
Build FAILED.
The folder part of both paths was shortened to ...; nothing else was changed.
Why it happens
Reading a value from the console and passing it straight on is the usual way in:
Console.Write("Your age: ");
string input = Console.ReadLine() ?? "";
PrintBirthYear(input);
static void PrintBirthYear(int age)
{
Console.WriteLine($"Born around {2026 - age}");
}
Console.ReadLine() returns text (a string, or null
when the input ends), because the user can type anything. C# never converts a string to an int on its own:
the conversion can fail ("thirty" is not a number), and the language makes failures you
must handle explicit. Column 16 of line 3 is the argument input. Watch for the
same pattern with any value that arrives as text, such as a line read from a file, and
convert it the same way.
The fix
Console.Write("Your age: ");
string input = Console.ReadLine() ?? "";
if (int.TryParse(input, out int age))
{
PrintBirthYear(age);
}
else
{
Console.WriteLine($"'{input}' is not a whole number.");
}
static void PrintBirthYear(int age)
{
Console.WriteLine($"Born around {2026 - age}");
}
int.TryParse returns true and fills age when the text
is a valid whole number, and false otherwise, so bad input becomes a message
rather than a crash. With 34 as input the program printed
Born around 1992; with thirty it printed
'thirty' is not a whole number.
int.Parse(input) also works, and is fine for text you control. With user
input it throws an exception for text that is not a number, so prefer
TryParse there.
How it was reproduced
A dotnet new console project on .NET SDK 10.0.403 (net10.0, nullable and
implicit usings on, no packages). dotnet build --no-incremental failed with the
CS1503 error above; the fixed version passed
dotnet build --no-incremental -p:TreatWarningsAsErrors=true and was run twice
from cmd with echo 34| and echo thirty| piped in.
Frequently asked
- How do I convert a string to an int in C#?
- Use int.TryParse(text, out int value) when the text may not be a number, such as user input; it returns false instead of throwing. Use int.Parse(text) only when the text is known to be a valid number.
- What does Argument 1 mean in CS1503?
- It is the position of the argument in the method call, counting from 1. Argument 1 is the first value inside the parentheses; that is the one whose type does not match the parameter.
- Why does Console.ReadLine not return a number?
- Console.ReadLine returns whatever the user typed as text, a string, or null when the input ends. Converting it to a number is your job, because only you know what to do when the text is not a number.
More decoded compiler errors are in the Fixes category.