C# Error
CS1929 – ‘{0}’ does not contain a definition for ‘{1}’ and the best extension method overload ‘{2}’ requires a receiver of type ‘{3}’
Reason for the Error & Solution
Instance argument: cannot convert from ‘typeA’ to ‘typeB’.
This error is generated when you try to invoke an extension method from a class that it does not extend. In the example shown here, the extension method is defined for the derived class A
, but not for the base class B
.
To correct this error
- Create a new extension method for the type where you have to invoke it, or else move the call into an object of the type that the existing method extends.
Example
The following code generates CS1928 and CS1929:
// cs1929.cs
using System.Linq;
using System.Collections;
static class Ext
{
public static void ExtMethod(this A a)
{
}
}
class A : B
{
}
class B
{
static void Main()
{
B b = new B();
b.ExtMethod(); // CS1929
}
}