C# delegate assignment inside ref struct

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

New contributor

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 structs 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 structs 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;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật