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.
public interface IData
{
int Value { get; set; }
}
public class Data : IData
{
public int Value { get; set; }
}

public class Container
{
public IData DataSlot { get; set; }
}

// In some method...

var container = new Container()
{
// How does this work? DataSlot is a interface.
DataSlot = { Value = 5 }
}
public interface IData
{
int Value { get; set; }
}
public class Data : IData
{
public int Value { get; set; }
}

public class Container
{
public IData DataSlot { get; set; }
}

// In some method...

var container = new Container()
{
// How does this work? DataSlot is a interface.
DataSlot = { Value = 5 }
}
7 Replies
Saber
Saber2y ago
it "works" as in it compiles, but it doesn't actually work
333fred
333fred2y ago
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:
var tmp = new Container();
tmp.DataSlot.Value = 5;
var container = tmp;
var tmp = new Container();
tmp.DataSlot.Value = 5;
var container = tmp;
So, in this case, what will actually happen is you get a null reference exception
MechWarrior99
MechWarrior992y ago
Oh, looks like it does work if DataSlot is assigned in the constructor of Container
333fred
333fred2y ago
Correct. Because it's just calling DataSlot.Value
MechWarrior99
MechWarrior992y ago
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!
333fred
333fred2y ago
np
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
Process.EnableRaisingEvents does not work on WindowsI have an app that have 20 workers that are other processes. On production, on ubuntu, when I close Top-level statements in MSVSSay you've created a new project with top-level statements in MSVS. Is it possible to change it to *how to format multiple conditions assignments in return statement [CLOSED] [Answered]Greetings, I was wondering, how could I properly format this piece of code. ```cs return res > PagEntity Framework One-To-One [Answered]Code at: https://paste.mod.gg/rccwzntafwoj/0 When attempting to change the database I'm getting: TDatabase - Relationship returns empty set even though records are present [Answered]I think I'm probably missing something stupid obvious... In my batch model, I have ```cs private reaSave storageFile to a folder on driveI have a storagefile ready to give to a control to crop an image the problem is that image crop wilDynamically load .dll into .NET 6 ProgramHi, I've a Blazor WebAssembly Hosted (.NET 6) web application. I've put some part of the applicationMove File To A Matching Type RefactoringWhen everything is top-level do you not need namespaces because everything is "top-level"? Thanks foClass making my program run as a background process after I close it [Answered]https://paste.myst.rs/7o8qz5vk After I close my program through RMB>Close window it continues runniHangfire - Unable to resolve services [Answered]Working with Hangfire. I have a plugin service which has an InitializeAsync method that looks like t