C# Error CS1113 – Extension method ‘{0}’ defined on value type ‘{1}’ cannot be used to create delegates

C# Error

CS1113 – Extension method ‘{0}’ defined on value type ‘{1}’ cannot be used to create delegates

Reason for the Error & Solution

Extension methods ‘name’ defined on value type ‘name’ cannot be used to create delegates.

Extension methods that are defined for class types can be used to create delegates. Extension methods that are defined for value types cannot.

To correct this error

  1. Associate the extension method with a class type.

  2. Make the method a regular method on the struct.

Example

The following example generates CS1113:

// cs1113.cs  
using System;  
public static class Extensions  
{  
    public static S ExtMethod(this S s)  
    {  
        return s;  
    }  
}  
  
public struct S  
{  
}  
  
public class Test  
{  
    static int Main()  
    {  
        Func<S> f = new S().ExtMethod; // CS1113  
        return 1;  
    }  
}  

Leave A Reply

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

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