With a combination of array and object initializer syntax you can do it all in one statement, this is entirely appropriate for instantiating POCOs.
var oHotel = new Hotel
{
Id = 100
room =
{
new Hotel.Room { RoomId = 1, RoomTypeid = 1, Name = "Name" }
}
};
If want to initialize your hotel with mith multiple rooms,
var oHotel = new Hotel
{
Id = 100
room =
{
new Hotel.Room { RoomId = 1, RoomTypeid = 1, Name = "A" },
new Hotel.Room { RoomId = 2, RoomTypeid = 1, Name = "B" },
new Hotel.Room { RoomId = 3, RoomTypeid = 2, Name = "C" },
new Hotel.Room { RoomId = 4, RoomTypeid = 2, Name = "D" }
}
};
As an aside, your
public Room[] room { get; set; }
property, should probbably be called Rooms.
If you don't wan't to use POCOs, like you show in you question, I'd rewrite your class like this, making it immutable,
public class Hotel
{
private readonly int id;
private readonly IList<Room> rooms;
public Hotel(int id; IEnumerable<Room> rooms)
{
this.id = id;
this.rooms = rooms.ToList();
}
public int Id
{
get { return this.id; }
}
public IEnumerable<Room> Rooms
{
get { return this.rooms; }
}
public class Room
{
private readonly int id;
private readonly RoomType type;
private readonly string name;
public Room(int id, RoomType type, string name)
{
}
public int Id
{
get { return this.id; }
}
public RoomType Type
{
get { return this.type; }
}
public string Name
{
get { return this.name; }
}
}
public enum RoomType
{
// Valid Room Types Here,
// ...
}
}
then I'd instantiate it like this,
var oHotel = new Hotel(
100,
{
new Hotel.Room(1, Hotel.RoomType..., "A"),
new Hotel.Room(2, Hotel.RoomType..., "B"),
new Hotel.Room(3, Hotel.RoomType..., "C"),
new Hotel.Room(4, Hotel.RoomType..., "D")
});
still in a single statement but, more compact. The resultant object would be immutable, this has numerous benefits beyond the scope of the question.