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

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

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

 public class Employee
    {
        public Employee()
        {       
                


        
8条回答
  •  无人及你
    2021-02-08 04:32

    Yes, an object can contain references to other objects of the same class.

    And secondly, I wouldn't create a new Employee in the cunstructor but inject it like this:

    public class Employee
    {
        public Employee(Employee manager)
        {
            this.Manager = manager;
        }
    
        public String StaffID { get; set; }
        public String Forename { get; set; }
        public String Surname { get; set; }
    
        public Employee Manager { get; set; }
    }
    

提交回复
热议问题