HomeCSharpC# 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}’

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...