🔧 Error Fixes
· 2 min read
Last updated on

C# Cannot Implicitly Convert Type — How to Fix It


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 var to let the compiler infer types and avoid mismatches: var count = int.Parse(input);
  • Prefer TryParse over Parse for user input — it won’t throw on invalid data
  • Pay attention to nullable reference types (string? vs string) — 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