I have an object model that contains an Array of objects. The model looks like this:
public Request()
{
RequestId = String.Empty;
Name = String.Empty;
Loads = Array.Empty<Load>();
}
public Load()
{
LoadId = String.Empty;
IsLoaded = false;
}
I need to create a new Request() object but I am having trouble figuring out the proper way to add an object to the array portion.
This is what I have:
var requests = new List<Request>{
new Request(){
RequestId = "1",
Name = "ATLGA",
Loads= new Load[1]{Loads[0] = new Load{IsLoaded = true} }
}
I get an error: the name "Loads’ does not exist in the current context. I have tried different syntax, but I am unable to figure this out.
>Solution :
This is how you initialize your array to one object:
Loads = new[] { new Load { IsLoaded = true } }