Can a Custom C# object contain a property of the same type as itself?

后端 未结 8 1289
情深已故
情深已故 2021-02-08 04:03

If I have created the following Employee object (simplified)...

 public class Employee
    {
        public Employee()
        {       
                


        
8条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 04:29

    An object can indeed have a reference to an object of its own type.

    This is how most Node type objects are implemented.

    As for instantiation - you can pass in the Employee object to use as manager (passing in null for no manager). Constructors can have multiple overloads:

    public Employee(Employee manager)
    {
       this.Manager = manager;
    }
    

提交回复
热议问题