C# Error CS1929 – ‘{0}’ does not contain a definition for ‘{1}’ and the best extension method overload ‘{2}’ requires a receiver of type ‘{3}’

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

  1. 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  
        }  
    }  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...