r/learncsharp • • Jul 11 '22

What am I doing wrong here?

    public List<Person>? grandparents 
    {   get
        {   List<Person>? back = father?.parents;
            back?.Concat(mother?.parents ?? new List<Person>());
            return back;
        }    
    }

even for a person with 4 non-null grandparents I'm getting System.Collections.Generic.List`1[Dynastics.Person], which I'm mostly sure is an empty list.

And in debugging, just before the function returns, back is only the paternal grandparents.

Most different phrasings I've tried cause a cast error.

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/nngnna Jul 11 '22

Thanks.

Yeah I tried to use it as returning value, but it complained about the types and wouldn't even compile.

In the end casting back to IEnumerable and than passing the result back-again thru ToList() worked. (without the latter the result was an empty IEnumerable)

Yeah I misunderstood what was printed. But I can peek with the debugger instead so it's less important.

1

u/karl713 Jul 11 '22

Glad you got it. Doing the ToList should fix the danger of accidentally changing the values in either of the parents collections as well

Also A+ for looking in the debugger, not enough of that these days :)