Cycle in the struct layout that doesn't exist

后端 未结 5 1024
天命终不由人
天命终不由人 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

    Disclaimer: This may not achieve the goal of "struct like value type behaviour."

    One solution is to use an array of one item to essentially get a reference the recursively referenced structure. Adapting my approach to your code looks something like this.

    public struct info
    {
        public float a, b;
        public info? c
        {
            get
            {
                return cArray[nextIndex];
            }
            set
            {
                steps[nextIndex] = value;
            }
        }
        private info?[] cArray;
    
        public info(float a, float b, info? c = null)
        {
            this.a = a;
            this.b = b;
            this.cArray = new info?[] { c }
            this.c = c;
        }
    }
    

提交回复
热议问题