Cycle in the struct layout that doesn't exist

后端 未结 5 1025
天命终不由人
天命终不由人 2020-12-03 10:39

This is a simplified version of some of my code:

public struct info
{
    public float a, b;
    public info? c;

    public info(float a, float b, info? c =         


        
5条回答
  •  囚心锁ツ
    2020-12-03 11:02

    The real problem is on this line:

    public info? c;
    

    Since this is a struct, C# needs to know the inner info/s layout before it could produce outer info's layout. And the inner info includes an inner inner info, which in turn includes an inner inner inner info, and so on. The compiler cannot produce a layout because of this circular reference issue.

    Note: info? c is a shorthand for Nullable which is itself a struct.

提交回复
热议问题