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
-
Associate the extension method with a class type.
-
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;
}
}