0

I am in the learning curve of entity framework. I follow the tutorials in the entity framework in msdn. In the sample, entities are defined as follows;

public class Department 
{ 
    public Department() 
    { 
        this.Courses = new HashSet<Course>(); 
    } 
    // Primary key 
    public int DepartmentID { get; set; } 
    public string Name { get; set; } 
    public decimal Budget { get; set; } 
    public System.DateTime StartDate { get; set; } 
    public int? Administrator { get; set; } 

    // Navigation property 
    public virtual ICollection<Course> Courses { get; private set; } 
} 

public class Course 
{ 
    public Course() 
    { 
        this.Instructors = new HashSet<Instructor>(); 
    } 
    // Primary key 
    public int CourseID { get; set; } 

    public string Title { get; set; } 
    public int Credits { get; set; } 

    // Foreign key 
    public int DepartmentID { get; set; } 

    // Navigation properties 
    public virtual Department Department { get; set; } 
    public virtual ICollection<Instructor> Instructors { get; private set; } 
} 

public class Instructor 
{ 
    public Instructor() 
    { 
        this.Courses = new List<Course>(); 
    } 

    // Primary key 
    public int InstructorID { get; set; } 
    public string LastName { get; set; } 
    public string FirstName { get; set; } 
    public System.DateTime HireDate { get; set; } 

    // Navigation properties 
    public virtual ICollection<Course> Courses { get; private set; } 
} 

I only showed some of the classes focused on my question only. The full sample is here. My doubt is why they are used hashset in the constructor to create ICollection properties?

hashset performance is good in the set operations If the hashset is appropriate in these type of areas, then

why they initialized with List in the constuctor of 'Instructor' entity?

May be simply shows the 2 options. But if not please advice and also tell me any other general scenarios to use hashset. Here they returnes as ICollection, only for initialization they used hashset.

Akhil
  • 1,918
  • 5
  • 30
  • 74

1 Answers1

0

I believe they did this because a HashSet enforces uniqueness. So the courses for a department must be unique. But that may not be the case for an instructor

Eitan K
  • 837
  • 1
  • 17
  • 39