How does assigning default values when creating an instance work? [Answered]
I'm not sure how to ask this question. I also can't for the life of me remember what this style of instance creating is called, otherwise I would just look it up.
7 Replies
it "works" as in it compiles, but it doesn't actually work
That is called a nested member initializer, and the key thing is that it's not creating an instance of
IData
Here's what the code looks like, long form:
So, in this case, what will actually happen is you get a null reference exceptionOh, looks like it does work if
DataSlot
is assigned in the constructor of Container
Correct. Because it's just calling
DataSlot.Value
Interesting, I think I get it now. I think I was confusing it with it having the
new
word DataSlot = new DataSlot() { .. }
.
Thanks for the help!np
✅ This post has been marked as answered!