Cannot implicitly convert type 'string' to 'int'
What causes this
C# is strongly typed and doesnβt allow implicit conversions between incompatible types. Youβre trying to assign or pass a value of one type where a different type is expected, and C# canβt automatically convert between them.
Common scenarios:
- Assigning a string to an int variable
- Returning the wrong type from a method
- Passing the wrong argument type to a method
- Mixing up nullable and non-nullable types
- LINQ queries returning a different type than expected
Fix 1: Parse strings to numbers
string input = "42";
// β Cannot implicitly convert string to int
int count = input;
// β
Parse it
int count = int.Parse(input);
// β
Safer β handles invalid input
if (int.TryParse(input, out int count))
{
Console.WriteLine(count);
}
Other conversions:
double d = double.Parse("3.14");
bool b = bool.Parse("true");
DateTime dt = DateTime.Parse("2026-03-15");
Fix 2: Use Convert class
// Convert handles more edge cases
int num = Convert.ToInt32("42");
string s = Convert.ToString(42);
double d = Convert.ToDouble(42);
Fix 3: Explicit casting for compatible types
// Numeric types can be cast explicitly
double price = 19.99;
// β Cannot implicitly convert double to int
int rounded = price;
// β
Explicit cast (truncates decimal)
int rounded = (int)price;
// β
Or round properly
int rounded = (int)Math.Round(price);
Fix 4: Fix nullable type mismatches
int? nullableCount = GetCount(); // returns int?
// β Cannot convert int? to int
int count = nullableCount;
// β
Provide a default
int count = nullableCount ?? 0;
// β
Or use .Value (throws if null)
int count = nullableCount.Value;
// β
Or use GetValueOrDefault
int count = nullableCount.GetValueOrDefault();
Fix 5: Fix async method return types
// β Cannot convert Task<string> to string
string result = GetDataAsync();
// β
Await the task
string result = await GetDataAsync();
// β Method returns string but signature says int
async Task<int> GetCount()
{
return "42"; // wrong type
}
// β
Return the correct type
async Task<int> GetCount()
{
return 42;
}
Fix 6: Fix LINQ type issues
// β Select returns IEnumerable, not List
List<string> names = users.Select(u => u.Name);
// β
Add .ToList()
List<string> names = users.Select(u => u.Name).ToList();
// β FirstOrDefault returns nullable
string name = users.FirstOrDefault()?.Name;
// If Name is non-nullable string, this is string? β string mismatch
// β
Provide a fallback
string name = users.FirstOrDefault()?.Name ?? "Unknown";
How to prevent it
- Use
varto let the compiler infer types and avoid mismatches:var count = int.Parse(input); - Prefer
TryParseoverParsefor user input β it wonβt throw on invalid data - Pay attention to nullable reference types (
string?vsstring) β enable<Nullable>enable</Nullable>in your project - Read the full error message β it tells you exactly which types donβt match and where
Related: C# NullReferenceException β Object Reference Not Set β How to Fix It
Related: C# TaskCanceledException β A Task Was Canceled β How to Fix It