I’m trying to conditionally define a delegate inside of a ref struct based on some condition. This works inside of a regular struct and a class, but for some reason, a ref struct doesn’t allow it.
I get the generic No overload for ‘Method1’ matches delegate ‘CreateInstance’
Again, this works in a regular structs and classes… why not in ref structs?
public ref struct MyRefStruct<T>
{
delegate T MyDelegate(ReadOnlySpan<string> span);
MyDelegate _creator;
public MyRefStruct()
{
_creator = myCondition ? Method1 : Method2;
}
T Method1(ReadOnlySpan<T> span) { //...omitted }
T Method2(ReadOnlySpan<T> span) { //...omitted }
}
cws-apellegrino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
The delegate type takes a ReadOnlySpan<string>
, but your methods take ReadOnlySpan<T>
, so obviously there is no matching overload. For the rest of this answer, I will assume the delegate declaration says:
delegate T MyDelegate(ReadOnlySpan<T> span);
Instance methods of ref struct
s cannot be used in a method group conversion.
From the language spec,
It is a compile-time error if a ref struct type is used in any of the following contexts:
- …
- An instance method of a
ref struct
type shall not be captured by method group conversion to a delegate type.- …
These constraints ensure that a variable of
ref struct
type does not refer to stack memory that is no longer valid, or to variables that are no longer valid.
Recall that ref struct
s always live on the stack, and will not escape into the heap. Consider
class Foo {
public MyRefStruct<T>.MyDelegate f;
public void M() {
var refStruct = new MyRefStruct<T>();
f = refStruct._creator;
}
}
and suppose I do
var foo = new MyRefStruct<int>.Foo();
foo.M();
foo.f(someSpan);
If this code were allowed to compile, foo.f
would be referring an instance method on the refStruct
local variable created in M
. But this value is no longer valid because M
has already returned!
If Method1
/Method2
then accesses fields of the ref struct
, they will also be accessing values that are no longer valid.
With regular structs and classes, they are allowed to go into the heap, so can be stored inside the instance of Foo
.
This was already raised as an issue on github 6 years ago… – Misleading CS0123 error about delegates to ref structs
Sweeper’s example assumes public
delegate while OP had private
delegate. So as such it can’t really escape because consumers can’t get a reference to it (save reflection of course).
The main point for me is that to construct a delegate with an instance method, an object
needs to be passed to the delegate constructor.
And again from the docs about ref structs:
A ref struct can’t be boxed to System.ValueType or System.Object.
This boxing behavior can be observed with normal structs:
public struct NormalStruct{
public delegate int MyDelegate();
public MyDelegate del;
public int field = 5;
public NormalStruct() {
del = Method1;
// boxing with field = 5
}
int Method1() => field;
}
var st = new NormalStruct();
st.field = 42;
Console.WriteLine(st.del()); // not 42
As a work-around you could define static
methods that are to be assigned to the private delegate:
public ref struct MyRefStruct<T> {
delegate T MyDelegate(ref MyRefStruct<T> inst,
ReadOnlySpan<T> span);
MyDelegate _creator;
public MyRefStruct() {
_creator = Method1; // or Method2 some strategy
}
public T ExposedMethod(ReadOnlySpan<T> span) =>
_creator(ref this,span);
static T Method1(ref MyRefStruct<T> inst, ReadOnlySpan<T> span) => default;
static T Method2(ref MyRefStruct<T> inst, ReadOnlySpan<T> span) => default;
}