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

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