Skip to main content

How to Resolve Error "CS0826: No best type found for implicitly-typed array" in C#

The Compiler Error CS0826 is a Type Inference error. The message reads: "No best type found for implicitly-typed array".

In C#, you can create an array without specifying the type name by using the new[] { ... } syntax. The compiler analyzes the elements inside the curly braces to determine the array's type. However, for this to work, the compiler follows a strict rule: It looks for a type among the elements provided that all other elements can be implicitly converted to.

If the elements are incompatible (e.g., integers and strings), or if they share a base type that isn't explicitly present in the list, the compiler gives up and raises CS0826.

Understanding Array Type Inference

When you write var x = new[] { A, B, C };, the compiler effectively asks:

  1. Can B and C convert to Type A?
  2. Can A and C convert to Type B?
  3. Can A and B convert to Type C?

If the answer is "No" to all questions, the error occurs. Crucially, the compiler does not guess a common ancestor (like object or a base class) unless that ancestor is explicitly present in the list.

Scenario 1: Mixing Totally Incompatible Types

This is the most obvious cause. You try to mix types like numbers and text in the same array. Since an int cannot become a string, and a string cannot become an int, inference fails.

Example of error

public class Program
{
static void Main()
{
// ⛔️ Error CS0826: No best type found for implicitly-typed array.
// Compiler checks:
// 1. Can "Hello" become int? No.
// 2. Can 10 become string? No.
var mixedArray = new[] { 10, "Hello" };
}
}

Solution: Use Explicit Typing

If you really need to mix types, you must explicitly tell the compiler to use a common parent type, usually object or dynamic.

public class Program
{
static void Main()
{
// ✅ Correct: Explicitly define the array as object[]
object[] mixedArray = new object[] { 10, "Hello" };

// Alternatively, cast one element to guide the compiler:
var explicitArray = new[] { (object)10, "Hello" };
}
}

Scenario 2: Shared Base Classes or Interfaces

This is a common "gotcha." You have two classes, Dog and Cat, which both inherit from Animal. You put a Dog and a Cat in an implicit array. You expect the compiler to figure out they are both Animals. It won't.

Unless there is an actual Animal instance in the list, the compiler only sees Dog and Cat. Since Dog cannot convert to Cat (and vice-versa), it fails.

Example of problem

public abstract class Animal { }
public class Dog : Animal { }
public class Cat : Animal { }

public class Zoo
{
public void BuildCage()
{
// ⛔️ Error CS0826: No best type found.
// Compiler logic: "Dog is not a Cat. Cat is not a Dog. I give up."
// It does not automatically infer 'Animal[]'.
var pets = new[] { new Dog(), new Cat() };
}
}

Solution: Explicit Type or Casting

You must either declare the array type explicitly or cast at least one of the elements to the base type so the compiler picks it up as the "best type."

public class Zoo
{
public void BuildCage()
{
// ✅ Option A: Explicit declaration (Clearer)
Animal[] pets1 = new Animal[] { new Dog(), new Cat() };

// ✅ Option B: Cast one element (Compiler infers Animal[])
var pets2 = new[] { (Animal)new Dog(), new Cat() };
}
}

Scenario 3: Arrays containing only null

The keyword null does not have a type in C#. It represents the absence of a reference. If you create an array filled only with null, the compiler has no type information to use for inference.

Example of error

public class Data
{
public void Load()
{
// ⛔️ Error CS0826: 'null' has no type.
var strings = new[] { null, null };
}
}

Solution: Specify the Type

You must tell the compiler what kind of empty slots these are (e.g., strings, objects, etc.).

public class Data
{
public void Load()
{
// ✅ Correct: We declare that these nulls belong to a string array
string[] strings = new string[] { null, null };

// Or implicitly via casting one element
var explicitStrings = new[] { (string)null, null };
}
}

Conclusion

CS0826 means the compiler refused to guess the Least Common Denominator for your types.

  1. Implicit typing (new[]) only works if one of the elements inside the array is the target type for all others.
  2. Inheritance: If mixing subclasses (Dog, Cat), you must explicitly cast one to the Base class (Animal) or define the array as new Base[].
  3. Primitive Mixes: If mixing int and string, define the array as object[].