Skip to main content

How to Resolve Error "CS0126: An object of a type convertible to 'type' is required" in C#

The Compiler Error CS0126 is a type-safety error that occurs when you use a C# keyword or statement that strictly requires a specific base type, but you provided an incompatible object.

The most common occurrence of this error is when using the throw statement. In C#, unlike C++ or JavaScript, you cannot throw arbitrary strings, integers, or enums as exceptions. You can only throw objects that inherit from System.Exception.

This guide explains how to resolve this error in exception handling and iterator contexts.

Understanding the Requirement

Certain language features in C# impose strict type constraints:

  • throw: Requires an object derived from System.Exception.
  • yield return: Requires an object convertible to the T defined in IEnumerable<T>.

If you try to pass a type that does not fit these requirements, the compiler raises CS0126 because it cannot convert your object to the required type.

Scenario 1: Throwing Strings or Numbers

This is the primary cause of CS0126. Developers coming from other languages often try to throw a simple error message string directly.

Example of error

public void ProcessData(string data)
{
if (string.IsNullOrEmpty(data))
{
// ⛔️ Error CS0126: An object of a type convertible to 'System.Exception' is required
// C# does not allow throwing string literals directly.
throw "Data cannot be empty";
}
}

Solution: Wrap in an Exception

You must instantiate a class that derives from System.Exception (like Exception, ArgumentException, or a custom exception) and pass your string to its constructor.

using System;

public void ProcessData(string data)
{
if (string.IsNullOrEmpty(data))
{
// ✅ Correct: Create a new Exception object containing the message
throw new ArgumentException("Data cannot be empty", nameof(data));
}
}
note

Custom Exceptions: You can throw your own custom objects, but only if you define them properly: public class MyError : Exception { ... }.

Scenario 2: Iterator Type Mismatch (yield return)

When writing an iterator method using yield return, the value you yield must match the generic type specified in the method signature.

Example of error

The method promises to return a list of integers, but the code tries to yield a string.

using System.Collections.Generic;

public class Generator
{
// Method signature says: "I will return Integers"
public IEnumerable<int> GetNumbers()
{
yield return 1;
yield return 2;

// ⛔️ Error CS0126: An object of a type convertible to 'int' is required
// "Three" is a string, not an int.
yield return "Three";
}
}

Solution: Match the Type

Ensure the yielded value matches the return type, or parse/convert it before yielding.

using System.Collections.Generic;

public class Generator
{
public IEnumerable<int> GetNumbers()
{
yield return 1;
yield return 2;

// ✅ Correct: Yield an integer
yield return 3;
}
}

Conclusion

CS0126 enforces strict rules on specific C# statements.

  1. Check throw statements: Ensure you are throwing new Exception("Message") and not just "Message".
  2. Check yield return: Ensure the data you are yielding matches the IEnumerable<Type> defined in the method header.