For example, in the following classes I need to have a Parent property in Cupboard and Shelf. How do I do it?
public class Room
{
public List<Cupboard> Cupboards { get; set; }
}
public class Cupboard
{
public Room Parent
{
get
{
}
}
public List<Shelf> Shelves { get; set; }
}
public class Shelf
{
}
You can use an automatically implemented property:
You can also choose to make the setter private and set it in the constructor.
Usage:
If you have many objects that all have a parent room you might want to create an interface so that you can find out which room is an object’s parent without having to know its specific type.