Compiler Error CS1929
'typeB' does not contain a definition for 'method' and the best extension method overload 'typeC.method' requires a receiver of type 'typeA'
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 D
, 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
- move the call into an object of the type that the existing method extends.
Example
The following code generates CS1929:
static class Extension
{
public static void ExtensionMethod(this D d)
{
}
}
class D : B
{
}
class B
{
static void Main()
{
B b = new B();
b.ExtensionMethod(); // CS1929
}
}
The following code solves the CS1929 as described in 1. - by creating a new extension method for proper type 'B':
static class Extension
{
public static void ExtensionMethod(this D d)
{
}
public static void NewExtensionMethod(this B b)
{
}
}
class D : B
{
}
class B
{
static void Main()
{
B b = new B();
b.NewExtensionMethod();
}
}
The following code solves the CS1929 as described in 2. - moving the call into an object of the proper type 'D':
static class Extension
{
public static void ExtensionMethod(this D d)
{
}
}
class D : B
{
}
class B
{
static void Main()
{
D d = new D();
d.ExtensionMethod();
}
}
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for